Ajax multiple response - report while proccessing
I am using ajax (jquery) to post items to my database.
before it posts to the database my script checks if some things, for example: first, it checks if everything is ok with the 开发者_如何学编程rules I gave, second it checks if the submitted picture already exist in the DB, third it checks if there's duplicate by calculating similar_text to items at the same date.I would like to know how can I report each time the php echo and not only at the end.
I'll know in which step I am because the first echo will be "1", the second will be "2" and the third will be "3" (1,12,123 echo append the text).
Thanks.
The way you describe it, it's not possible. IF you want multiple responses you will need multiple requests. You cannot have one ajax call return multiple responses.
You could however break it up into multiple requests. First send an ajax request to check validation rules, a 2nd to see if it already exists, etc. And then you can have each callback call the next ajax request in the chain.
You just need a <div>
in your page into which you can .append
the current progress in the .success()
callback of each of the AJAX posts.
<div id="progress">
$.ajax( call1 ).success(function(resp) {
// do something with resp
$('#progress').append('step 1 done<br/>');
$.ajax( call2 ).success(function(resp) {
// do something with resp
$('#progress').append('step 2 done<br/>');
}
etc...
精彩评论