By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,600 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,600 IT Pros & Developers. It's quick & easy.

Nested AJAX Calls Problem

P: 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
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
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

P: 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

P: 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

Post your reply

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