473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using Ajax routine in for loop only returns last result?

Hi,

I'm not very good with javascript/ajax but managed to get this
example working:
http://www.dynamicdrive.com/dynamici...jaxroutine.htm

I've adjusted their getAjaxRequest function slightly to allow me to
specify where the result goes 'divtoupdate' which is just an
extra argument to the function.

What i'm trying to do now is have a button on a website that runs
a loop updating a number of DIVS, eg:

for (x in divs_to_update)
{
updatediv = divs_to_update[x];
ajaxpack.getAjaxRequest("example.php?id=" + updatediv, "", processGetPost, "txt", updatediv);
}

This works OK except only the last DIV in the loop actually
gets updated. I think this has something to do with the example
code using global objects or something.

Can anyone please suggest what I need to change to have each
call in the for loop actually actioned separately?

Thanks a million,
Adrian.
Jul 9 '07 #1
6 1944
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Either wait for one to finish (check that readystate is 4) before making the next request or use a new object for the next request.
Jul 9 '07 #2
Hi Acoder,

Thanks for your reply. I think a new object is the going to
be the answer. There's a line in the ajaxroutine.js
'var ajaxpack=new Object()'.

Do I need to somehow move all this code into my for
loop so it creates a new object each cycle? Can you
give me any tips on how that might work?

Alternatively how would do I get the scripts to sit
and wait until myajax.readyState == 4 ?

Thanks,
Adrian
Jul 10 '07 #3
acoder
16,027 Expert Mod 8TB
It depends on whether you want the requests to run concurrently or not.

If they can run one after another, just wait for the readyState to equal 4 (ready). You can see how that's done in the examples. You use onreadystatechange to detect changes in the state. Call a function which checks that its 4. If it is, take the response and update the div, then make another request. You could make this generic by having an array and just taking the next one until there are no more array items.

As for the objects, there is a line:
Expand|Select|Wrap|Line Numbers
  1. ajaxpack.ajaxobj=createAjaxObj()
Now if you could add more ajaxobjs, you could make concurrent requests.
Jul 10 '07 #4
opps - please see post below instead.
Jul 11 '07 #5
It depends on whether you want the requests to run concurrently or not.

If they can run one after another, just wait for the readyState to equal 4 (ready). You can see how that's done in the examples. You use onreadystatechange to detect changes in the state. Call a function which checks that its 4. If it is, take the response and update the div, then make another request. You could make this generic by having an array and just taking the next one until there are no more array items.

As for the objects, there is a line:
Expand|Select|Wrap|Line Numbers
  1. ajaxpack.ajaxobj=createAjaxObj()
Now if you could add more ajaxobjs, you could make concurrent requests.
Hi, I'm still struggling to get either method working. I'm going with the sequential option (though concurrent would be better if it's easy to do) but this solution doesn't work.. Can you see where i'm going wrong? The last argument to my getAjaxRequest function is just the name of the div that gets updated with the response.. That's all working fine, just not in the loop!

Expand|Select|Wrap|Line Numbers
  1. var joblist = new Array('google','yahoo','msn');
  2.  
  3. process_all()
  4. {
  5.     // Count how many jobs there are to run still
  6.     for (x in joblist)  { count++; }
  7.  
  8.     if(count>=1)
  9.     {
  10.          // Get a new job to run
  11.          var job = joblist[count-1];
  12.  
  13.          // pop it off the list so we don't see it again
  14.          joblist.pop();
  15.  
  16.          // Perform Search using AJAX call to PHP script
  17.          ajaxpack.getAjaxRequest('search.php', '', processGetPost, 'txt', job);
  18.  
  19.          // On completion,  run the search again (on the next JS array item)
  20.          ajaxpack.onreadystatechange=process_all();
  21.      }
  22. }
  23.  
Thanks,
Adrian
Jul 11 '07 #6
acoder
16,027 Expert Mod 8TB
If you're confident enough to use Ajax without a library/framework, this should be quite straightforward. It's not that difficult if you look at the tutorials in the Offsite Links thread (see sticky at top of this forum). Call a function and use
Expand|Select|Wrap|Line Numbers
  1. var ajaxobj = new XMLHttpObject(); //or whatever creates object
That should create a new object every time.
Jul 11 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: PG | last post by:
When deleting a row from the database, that id is now missing. So what I'm trying to do is update in a loop (maybe an sql loop if there is one) of all of the id numbers - sort of like renaming...
4
by: Japhy | last post by:
Hello, I'm am pulling data from a mysql db and want to use the data to populate a <ul. Here are relavent parts of my code : $wohdate = mysql_result($wohRS,$wohndx,woh_date); $woh_display...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.