I'm making a simple JS which consumes a Rest api. Basically the user types in a city, clicks submit and based on that JS generates a GET call. Then using jQuery the script inserts the received data into HTML document. So every time a user clicks submit I want the contents of ny HTML to be filled with new data from te API (without refreshing the page of course). My plan was to structure the script in a way that firstly JS deletes all the prievious content from HTML (run .empty() on each element) and afterwards add new data. Is there an easier way of doing that?
Related
As the title suggest: Is it best practice to create HTML elements from inside external POST file or from within calling file?
Specifically, I am creating a wizard with PHP and jQuery. I am looking for help with fundamentals of structure when it comes to proper usage of passing data to an external page to process it.
The process goes as such: The user is on a page, "wizard.php". First, Step One is displayed, which asks them to enter a title of their Project in an input text element. A "Next" button is displayed as well.
Once this is done, the user clicks "Next", and a $(.post) method is invoked, and in an external file (wizard_process.php), the "title" is sanitized and added to the database.
Now, once the title has been sanitized, I want to display to them "Step Two", which is going to provide the Sanitized Title that they have input in the previous step, a few more input checkboxes (all inputs that are needed for Step Two), and other criteria for them to fill out.
Do I want to
a) print the HTML elements from within the "wizard_process.php" so that it is displayed on the parent, "wizard.php", page? (it would be a <h1>$sanitized_title</h1>) along with the next set of checkboxes and inputs. It would include all elements of Step Two coded as HTML in the wizard_process.php.
b) send back json data (not in HTML form, but in json form) and only use the external php file (wizard_process.php) to sanitize and add to database and return json data. {sanitized_title: $sanitized_title} and then create the HTML element to display that data from within the calling jQuery function? (wizard.php)($(".successresponse").html(result);) and then create the next set of checkboxes (Step Two inputs) from the calling page (wizard.php) as well?
Or do I want to
c) send back json data (not in HTML form, but in json form) and then make already created elements visible. For instance, having all the textboxes, questions, checkboxes, and drop downs from the entire wizard hard-coded into the HTML file, with the hidden option = true until we reach that phase of the wizard.
I know each of these is possible, but being a PHP coder, my first instinct would be to use the wizard_process.php as sort of a Controller/Model (MVC) so as to only process data, and not use it as the View. That means not actually creating the HTML elements inside that external post process file, but rather only return data to the calling function to then later format and display in HTML.
What would be the best way to handle this?
I am trying to add multileg travel information using PHP, jQuery and MySQL. I present to users with existing data and then they dynamically add travel leg information to their existing visit. What I have done so far is below.
Bind data (PHP -MySQL) to the HTML table. Show add new button the user.
When the user clicks on add new, I have used jQuery to clone the data and this has dynamic id.
On the form there is file upload as well which needs to upload the file and also save information in the database.
My problem is how can I rebind the data on the table without effecting any other thing. I have tried to use jQuery load but it tries to reload the entire page rather than just reloading the newly generated trs.
I am open to change my logic on the whole as to how I do this.
Appreciate any help on this.
The only way I know of to change the text on an HTML doc is to use javascript (jQuery). Basically I'm trying to make a really simplified "cart" where I have a page with 5 links on it and whenever you click one of them it will add 1 to the "items in cart" counter on the page without having to reload the entire page.
I have a java class Cart that stores the current number of items in the cart and the names of the items in the cart. I can easily make it so clicking a link/button on the page would add an item to this object, but what I can't figure out how to do is change the items number that is displayed on the page without reloading the entire page.
What is the best way to do this?
invoke a AJAX request (i.e. background javascript) from the web page to the server every 1 min, to get latest information. You can use Jquery to invoke AJAX request.
There should be Jquery sample code available via google to execute AJAX request to get new data, and also samples code to change HTML using the new data.
Is there a way to permanent append an anchor that resulted from a form.
I am current doing a project that enable a user to have multiple profiles. Is there a way that enables the user after creating a form to click on a generated link which will to profile that will always be on the page.
pushing the data to the database and retrieving it for the profile is understandable but creating the links for the profiles using AJAX or JQUERY is a real brain teaser.
When I open a blank form to add new row to the grid, I need some fields on the form to be dynamically updated by going to the database to search for data if I change a field on the form.
I found the code in the javascript to create a form for adding new row, may I change it to read from an external file (like an asp, php page) or is there any way to do it properly??
You must use ajax to make calls to proper functions and change your html according to response data.
More info about ajax
Jquery supports ajax and have many nice features for ajax usage and let you have nice page alteration based on html DOM. So i advice you to have a look at it...