473,385 Members | 1,693 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.

Nested AJAX Calls Problem

13
I am trying out nested AJAX calls for the first time, but I seem to have hit a snag. The code snippet is the outer AJAX call and a function, it gathers information about a company.
As we get towards the bottom of the onComplete function, there is a call to doGetAddressContacts(), which accepts an address ID. This then returns a list of contacts at that address.
What I am trying to do is for each address that it retrieves and outputs, as it gets down to the fax, I want to do another AJAX call to retrieve all contacts for that address, then output the results in the line after the fax.

What happens when I try this is it gathers contacts for each address, puts everything in its own line fine, but it outputs EVERYTHING after all of the addresses.

I.E.
Address 1
Blah
Blah
Address 2
Blah
Blah
Address 3
Blah
Blah
Contacts for Address 1: xxxxxxx
Contacts for Address 2: xxxxxxx
Contacts for Address 3: xxxxxxx

When I want the output to be:
Address 1
Blah
Blah
Contacts for Address 1: xxxxxxx
Address 2
Blah
Blah
Contacts for Address 2: xxxxxxx
Address 3
Blah
Blah
Contacts for Address 3: xxxxxxx

I apologize for the long and probably confusing post, but if anyone can understand this madness I thank you for your help.

I am using the Mootools Library 1.11 by the way.

Expand|Select|Wrap|Line Numbers
  1. var companyAddrURL = "xxxxxxxx.php?companyID=" + companyID;
  2. /****MORE DETAILED COMPANY LOOKUP (ADDRESSES)****/
  3. new Ajax(companyAddrURL, {
  4.         method: 'get',
  5.         onComplete: function(request) {
  6.             //Split return string on unique delimiter into array
  7.             var addrArray = request.split("|||");
  8.             //First element contains error message if no addresses were found
  9.             if(addrArray[0] == "No addresses found for this company.") {
  10.                 $('companyDiv').innerHTML += addrArray[0]; 
  11.             }
  12.             else { //At least one address was found
  13.                 $('companyDiv').innerHTML += "<center><b>Addresses For This Company</b></center>";
  14.                 /****ADDRESS INFORMATION OUTPUT****/
  15.                 for(var i=0; i<addrArray.length; i++) {
  16.                     var addrObj = Json.evaluate(addrArray[i]);
  17.                     $('companyDiv').innerHTML += "<b>Address " + (i+1) + ": </b><br />"
  18.                     //Address 1
  19.                     if(addrObj.addr1 != "") {
  20.                         $('companyDiv').innerHTML += addrObj.addr1 + "<br />";
  21.                     }
  22.                     //Address 2
  23.                     if(addrObj.addr2 != "") {
  24.                         $('companyDiv').innerHTML += addrObj.addr2 + "<br />";
  25.                     }
  26.                     //City, state, zip
  27.                     $('companyDiv').innerHTML += addrObj.city + " " + addrObj.state + " " + addrObj.zip + "<br />";
  28.                     //Country
  29.                     if(addrObj.cntry != "") {
  30.                         $('companyDiv').innerHTML += addrObj.cntry + "<br />";
  31.                     }
  32.                     //Phone
  33.                     if(addrObj.phone != "") {
  34.                         if(addrObj.ext != "") {
  35.                             var extString = " Ext " + addrObj.ext;
  36.                         }
  37.                         else {
  38.                             var extString = "";
  39.                         }
  40.                         $('companyDiv').innerHTML += "<b>Phone: </b>" + addrObj.phone + extString + "<br />";
  41.                     }
  42.                     //Fax
  43.                     if(addrObj.fax != "") {
  44.                         $('companyDiv').innerHTML += "<b>Fax: </b>" + addrObj.fax + "<br />";
  45.                     }
  46.  
  47.                     //Add an extra line break to separate the addresses more clearly
  48.                     $('companyDiv').innerHTML += "<br />";
  49.                     doGetAddressContacts(addrObj.addressid);  // <==Function call here
  50.                 }
  51.                 //Add separator and get opportunities information
  52.             }
  53.         }
  54. }).request();                            
  55.  
  56. function doGetAddressContacts(addrID) {
  57.     //Contacts at this address
  58.     var contactsURL = "xxxxxxxx.php?addrID=" + addrID;
  59.     /****CONTACTS ASSOCIATED WITH THIS ADDRESS****/
  60.     new Ajax(contactsURL, {
  61.             method: 'get',
  62.             onComplete: function(request) {
  63.                 if(request == "No contacts for this address.") {
  64.                     $('companyDiv').innerHTML += request + addrID + "<br />";
  65.                 }
  66.                 else {
  67.                     $('companyDiv').innerHTML += "<b>Contacts for addrid: </b>" + addrID;
  68.                     var contactsArray = request.split("|");    
  69.  
  70.                     //Loop through all but last entry, URL encode each name, and add a comma to the end
  71.                     for(var i=0; i<contactsArray.length-1; i++) {
  72.                         var namesArray = contactsArray[i].split(" ");
  73.                         //First name
  74.                         var firstName = namesArray[0];
  75.                         //Check if there's a last name for this contact
  76.                         if(namesArray[1]) {
  77.                             var lastName = namesArray[1];
  78.                         }                                                            
  79.                         //If last name exists, the link is different than if not
  80.                         if(namesArray[1]) {
  81.                             $('companyDiv').innerHTML += '<a href="xxxx.php?method=byName&fName=' + firstName + '&lName=' + lastName + '">' + firstName + " " + lastName + "</a>, ";
  82.                         }
  83.                         else {
  84.                             $('companyDiv').innerHTML += '<a href="xxxx.php?method=byName&fName=' + firstName + '">' + firstName + "</a>, ";
  85.                         }
  86.                     }
  87.                     //Add last entry to the list, URL encoded but without a comma at the end
  88.                     var namesArray = contactsArray[contactsArray.length-1].split(" ");
  89.                     //First name
  90.                     var firstName = namesArray[0];
  91.                     //Check if there's a last name for this contact
  92.                     if(namesArray[1]) {
  93.                         var lastName = namesArray[1];
  94.                     }
  95.                     //If last name exists, the link is different than if not
  96.                     if(namesArray[1]) {
  97.                         $('companyDiv').innerHTML += '<a href="xxxx.php?method=byName&fName=' + firstName + '&lName=' + lastName + '">' + firstName + " " + lastName + "</a>";
  98.                     }
  99.                     else {
  100.                         $('companyDiv').innerHTML += '<a href="xxxx.php?method=byName&fName=' + firstName + '">' + firstName + "</a>";
  101.                     }                                                        
  102.                 }
  103.                 $('companyDiv').innerHTML += "<br />";
  104.             }
  105.     }).request();    
  106. }                        
  107.  
Oct 9 '07 #1
3 3919
acoder
16,027 Expert Mod 8TB
You're just appending to the companyDiv element. You need to include a span or something and a reference to that span and append to that rather than the containing div element.
Oct 10 '07 #2
dhsieh
13
I was trying to set it up so that the function inside the for loop would append the information onto each address entry after the Fax line. Do I not have that set up correctly? Thank you for your reply.
Oct 10 '07 #3
dhsieh
13
It seems like I've created a closure with the AJAX call being in the for loop, and that's the reason why everything is returned at once at the end of all loop cycles instead of once per. Can anyone shed some light on how to fix this?
Oct 11 '07 #4

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

Similar topics

6
by: A | last post by:
Hi, How do you make use of nested functions in C++? I realize in C++ that everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method...
2
by: Quinnie | last post by:
Hi, I have a homework assignment that I'm so confused and really need help with. Here's the description, any help would be appreciated. Thanks! Assume we have a statically-scoped language...
1
by: r_o | last post by:
hi all, i have a new problem now i'm quick huh :D well what can i say im still a beginner the thing is im using ajax to replace a drop down list with another inside a div upon a choice made by...
17
by: Arjen | last post by:
Hi, I want to reload 2 divs at one click. Ive tried: <a href = "javascript:void(0);"...
5
by: steve.chambers | last post by:
I'm sure this q must have been asked before but I'm really struggling to find the answer anywhere so have finally given up and will consult the usenet community - hopefully there's someone out...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
3
by: nghivo | last post by:
I attempted to synchronize async Ajax calls using the following JS blocks: ==================================================== function getXMLHTTPRequest() { try { req =...
1
by: bizt | last post by:
Hi, I am having my first real attempt at an ajax class as so far Ive managed to build one that, once instatiated, will allow me to define which function it will call on completion then retrieves...
1
by: EnjoyNews | last post by:
Hi I have a little problem with java ajax... On my site I have a google adsence placed in a list. After the first 10 records it shows a adsence commecial. But.. on my search page I use Ajax...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.