AJAX Call one after anohter


I have a situation, where some records will be searched from a database, and then is the process button is clicked, an ajax process will initiate that will process the first row, when the first row is done, then it will automatically initiate the ajax process for row 2 and so on.

I want to know what is the best way to do this ?

What i have in my mind is that, passing all the rows id in via ajax to the script and then script process and return the next id, and on the reply, if there is an id, then another ajax will initiate the process with this id, and then so on.

For example,

here are the ids, 2,3,4,5,7,8,9

i will sent this to the php script like this

request.open('get', "process.php?this_id=2&ids=2|3|4|5|7|8|9")

Process.php will process 2, and will also return the next id (3) and all the ids (2|3|4|5|7|8|9) so that on reply, the next ajax will initiate with


and so on,

Is this correct approach ?

Hello ScallioXTX

Thanks for your suggestion and nice reply. Well, the number of rows can be 1-50. But the point is that this thing is for the admin of a site and not for the users. So at a time, there will be only 1 user doing all this and that is admin.

For the same reason, there is no alternatives if Javascript is not enabled. As the admin knows what he needs to run this script.

I agree that batch thing is good. I want to know that when the xml or JSON is sent as a response from AJAX, how Javascript can handle that to show ?

Despite the fact that batch thing is good, having AJAX doing it row by row and presenting Live updates will create a good user interface experience and with this it will give an impression that the page is live.

In the case of Admin, do you think that using Live method is ok ?

I would use either XML or JSON to achieve this. Plain text is too fragile to do this kind of thing.


  <status for="2">Complete</status>
  <status for="3">Complete</status>
  <status for="4">Complete</status>

JSON can be built similarly.
Note that the results won’t update real time if you do it this way, but the speed advantage you have of 1 ajax call instead of n ajax calls totally makes that worth it (imho).
How many rows are we talking about (on average)?
If it’s 3 or 4 your solution is also OK.
Although I don’t think I would implement a queue, but just fire all AJAX calls at the same time (if the processing of row n+1 does not depend on the processing of row n that is).
If it’s more than 3 or 4 rows I would go for the batch update.

PS. Do you also make an alternative implementation for those with javascript disabled?
PPS. How is the AJAX initiated, does the user have to press a button or something?

actually, I want to show the LIVE status of process on each row.

If I do it like you said, how I can echo multiple ?

Is it possible like this ?

foreach($_GET[‘ids’] as $id)
if (!ctype_digit($id)) // make sure only numbers are requested

// process id $id
echo “$id##Status = Complete”

And then on the receiving Javascript, I will split the results so that, $id and “Status = Complete” will be in 2 separate variables, and based on id,

i.e. data = answer.split(“##”);
id = data[0];
status = data[1];
document.getElementById(“div_”+id).innerHTML = status ?

What do you think ?

Why not request process.php?ids=2&ids=3&ids=4&ids=5&ids=7&ids=8&ids=9

And then in process.php

foreach($_GET['ids'] as $id)
   if (!ctype_digit($id)) // make sure only numbers are requested

   // process id $id

I don’t see why you want to call php n times for n rows while 1 call should suffice (and is probably much faster to booth).


This is not something I can explain easily. You should probably read some articles/books on how to work with AJAX and XML/JSON.

I still think the live method is too slow, regardless of the arguments on why you’d want to use it.
You need to consider that a HTTP request will take about 30ms to connect, and an additional number of ms waiting for the answer. Let’s assume your php code is quite simple and can be processed in 100ms. And suppose that out of this 100ms, 50ms is needed for PHP to initialize request, and the remaining 50ms are needed for the processing itself.
Furthermore, suppose the table has 50 rows (worst case scenario is always good for these kind of calculations).

Using the live method you would need 50 connects, so 50x30=1500ms, and an additional 50x100ms=5000ms, adding to a grant total of 6500ms = 6.5 seconds

Using the batch update you need only one connect, 50x1=50ms, start the PHP process once, 50x1=50ms and process all 50 rows 50x50=2500ms, adding to a grant total of 2600ms = 2.6 seconds

Of course the numbers are fictional/estimates, but my point is that using the live method you’re using a lot of time of connecting to the server and firing PHP on each request, while the batch method does not suffer from this and is therefore also much more scalable. Consider the above calculation with 1000 rows!

If you’re familiar with the Big O notiation, the Live method runs in O(n), where n is the number of rows, whereas the batch method runs in O(1).