What are the advantages of using jQuery Ajax to POST data to the backend?
Is it necessary to use jQuery Ajax to POST if your html and php are on the same page? (meaning it only reloads, not redirect, after being submitted traditionally).
Whether to use jQuery or vanilla JavaScript is your choice. Ajax is useful if you just want to send a small size of data to the server without downloading the entire page from the server.
Suppose you have a page with 1000s of lines of HTML and you need to submit a simple yes or no response to a question to the server. Now, if you just redirect to the same page, it will still download most of the HTML page again. Ajax will just send that bit of data you want to send to the server and get a response which you can use within the client side to make changes to the page.
Related
I was trying to submit a HTML form that includes text fields and an image file using AJAX and jQuery. So it turns out you cannot do file upload using AJAX. Are there any nice ways to submit text form data along with a file? I'm aiming for broad browser compatibility, particularly with older IE versions.
One way I've tried is to eliminate using AJAX and simply use normal POST (although AJAX is the preferred way). After the POST another page loads. I can redirect the browser to load another page from my PHP using
header("Location: new_page.html");
but after this, how can I send the JSON response to the JavaScript script on new_page.html. If I do something like this, will the response now go to the JS script on new_page.html?
header("Location: new_page.html");
$data = json_encode(response);
echo $data
So it turns out you cannot do file upload using AJAX.
That isn't true
I can redirect the browser to load another page from my PHP using
The Location HTTP header accepts an absolute URI only. Most browsers will silently recover from that error, but you shouldn't let that be a reason to make it.
after this, how can I send the JSON response to the JavaScript script on new_page.html.
You don't. You generate that page programatically and build it in the state that the JavaScript would have put it in if you had run it after the page had loaded.
Loading a page that immediately fetches data with JSON to update itself is just cutting corners. You end up with the flash-of-default-content problem that used to plague Twitter (before they started building pages server side on initial load and using Ajax only to update them when the view changed).
I Want To Ask May I use Ajax to send data to multi pages phph
i have 2 buttons in an HTML from and i want each button to send data to different php pages using ajax from the same html page
Ya.. You can make this.. if the functionality is different, you need to give individual AJAX request for each page.. if the functionality is same, you can change the URL dynamically and send with the single AJAX request...
Is it possible to have a JSP/JSPX form to submit to a PHP file?
The PHP file will then be in charge of validation and database update. After that it will send a sort of response to the same JSP/JSPX form. Also this should be done in AJAX using jQuery.
How can I do that and what are the underlying concepts needed?
Thanks!
You don't need anything specific in your case. Just submit the form via Ajax using jQuery to the PHP page and process the result in JavaScript as well. All the code necessary for that will be client side in JavaScript and operate on the plain HTML generated by the JSP page. The only restriction/problem might be if these two pages run on different domains.
I have a PHP file that has an HTML form that submits via AJAX to the database. When I hit the form submit button, every PHP query updates itself. Is this how Ajax normally operates? or if I switch the parent file from PHP to HTML, will it eliminate the unwanted updating of all the PHP on the page?
jQuery ajax submits the request to a seperate php file to load data. You can choose to load only poritions of pages. $.load does this easier. Look here: http://api.jquery.com/load/
You said it submits via AJAX to the database - but have you actually programmed it to do that?
Post a sample of the AJAX code and we can check it for you.
I have a feeling you don't really understand the technology you are using. What is a 'PHP query'? You want to switch the page from PHP to HTML? Well, can you? Does the page have PHP in it or not? It wouldn't make any difference to AJAX code, but I have a feeling you don't actually have AJAX code.
Currently I'm doing this in two steps:
1.post it to ask.php
2.after inserting it into database,use header("REFRESH: 0;URL=post.html") to jump to the result page
But how to do it all in one step,say,like SO here?
SO does it using Ajax. But for the easier win, why not just use header('Location: http://example.com/post.html') instead of a refresh?
It happens using ajax, I'm guessing.
-User types post, hits submit.
-Post content is sent via ajax to the server where it tries to save it.
-If it is saved:
The post is added to the page using javascript and some pretty animations and all the various listeners are added to the clickable elements.
-If not:
Show some error.
I'm sure there's more to it than that, but that's probably the basic idea.
Instead of a that refresh header, after processing the POST request, tell the client to view the result with a Location redirection header
header("Location: http://www.example.com/post/$post_id");
That kind of thing is done with Ajax. Using javascript to request small(er) amounts of data from the server, then updating the page, bypassing a full page request/refresh.
Here's a few libraries that may help get you started with Ajax and PHP:
XAJAX
Zend_Json_Server (more complex)
PHP Ajax Example at W3Schools