Forum Moderators: open

Message Too Old, No Replies

Calling JavaScript after AJAX completes

AJAX complete call javascript timing

         

jrpmedia

6:59 pm on Oct 25, 2009 (gmt 0)

10+ Year Member



My AJAX is working fine and populating the correct DIVs, but...

One of the AJAX calls populates a form field that I am using so that I can grab a variable (sorry, I'm a newbie).

I then want to grab this variable and pass it on to a JavaScript function to use (which is on the main HTML page).

Problem is, the AJAX takes time to complete, and the JavaScript call fires off before AJAX has a chance to populate the field (supply my variable for me utilise).

If I call the AJAX a second time, all is well as the field (variable) is populated for use.

I know it;s crude solution, but I'm learning....

***********HERE IS MY JAVASCRIPT AJAX CODE ****************************
var ajaxRequest4;

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest4 = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest4 = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest4 = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}

ajaxRequest4.onreadystatechange = function(){
if(ajaxRequest4.readyState == 4){
var ajaxDisplay4 = document.getElementById('ajaxDivVideo');
ajaxDisplay4.innerHTML = ajaxRequest4.responseText;
}
}
// var fid = document.getElementById('fid').value;
var queryString4 = "?fid=" + fid;
ajaxRequest4.open("GET", "readurlfromdb.php" + queryString4, true);
ajaxRequest4.send(null);

loadNplay(document.getElementById('myrecurladdr').value);

***********HERE IS MY PHP CODE ****************************
<?php
include("config.inc");
mysql_connect($db_address, $db_username, $db_password);
mysql_select_db($db_name) or die(mysql_error());

$fid = $_GET['fid'];
$fid = mysql_real_escape_string($fid);

$query4 = "SELECT * FROM mydata WHERE (rec_id = '$fid')";
$qry_result4 = mysql_query($query4) or die(mysql_error());
while($row = mysql_fetch_array($qry_result4)){

$myrecurlstr = "http://www.abc.com/$row[rec_client]/$row[rec_client_office]/$row[rec_filename].jpg";
$display_string4 .= "<input name='myrecurladdr' type='text' id='myrecurladdr' value='$myrecurlstr'>";

}

echo $display_string4;
?>

Zipper

11:39 pm on Oct 25, 2009 (gmt 0)

10+ Year Member



You either need to implement a callback function or include the loadNplay() function call within the if(ajaxRequest4.readyState == 4){} clause.

jrpmedia

5:24 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



That's it !

I am smiling so much I cannot stop :-) :-) :-)
I'm a happy man.

...it seems so obvious now...

Thank you, would by you drinks if you were nearby :-)