Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX and a global variable not working, have I got this wrong?
    text
    copied!<p>what I am trying to achieve is to AJAX a load of client's data into a page (this works), I then have a company ID in one of the fields brought in. I need to cross check this with a different company table (same database) to replace the company ID on the page with the name instead.</p> <p>To get this I have set a global javascript variable to blank then fired off the main AJAX request getting all the initial client data then within that parsing loop (client side) I need to fire off a function which will check against the companies table to get the name. My current problem is that the global variable is not being set to the 2nd AJAX result. Here is my code:</p> <pre><code>var nameresult = ""; function namecheck(id){ var request = new Ajax().sendRequest ('../company_check.php', { method: 'GET', parameters: 'id=' + id, callback: namecheckReceived } ); } function namecheckReceived(xmlHTTP){ var n_data = JSON.parse(xmlHTTP.responseText); nameresult = n_data[0].name; } function client_call(){ var request = new Ajax().sendRequest ('../client_data.php', { method: 'GET', callback: searchReceived } ); } function searchReceived(xmlHTTP){ var data = JSON.parse(xmlHTTP.responseText); for(var i=0; i&lt;data.length; i++) { namecheck(data[i].company_id); /////spit out all the data in a readable format ////// } } </code></pre> <p>Notes: </p> <ol> <li>Only one result will be received from the company_check.php hence no loop in the namecheckRecieved() function. </li> <li>No errors in the JS console.</li> <li>The nameresult variable stays as blank and is never changed, if I alert(nameresult) within the namecheckRecieved() function it spits out what I want so why is it not changing the global variable with each loop of the searchRecieved() function?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload