can someone please tell me why this isn’t working?
For some reason I need more text for this amount of code… cant really see how this could benefit anybody but whatever have some more text…..
This:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("rS4Xs200");
var string33 = xmlhttp.responseText;
//document.write(xmlhttp.responseText);
return(string33);
}
}
var urlToGet = 'd2/two.php';
alert(urlToGet);
xmlhttp.open("GET",urlToGet,true);
xmlhttp.send();
}
Returns:
undefined
The reason it’s showing “undefined” is because the AJAX request is asynchronous and therefore your script calls the AJAXrequestDLC() function and eventually sends the xmlHttpp request, but it then moves on to the write command containing that function call before the onreadystatechange function is fired (because the response hasn’t been returned yet). You can verify this by changing your code to something like:
You’ll notice that the “boom” alert comes before the alerts in the onreadystatechange function in your code, indicating that the script has moved on while waiting for the response from your PHP file.
EDIT: Rewritten this answer twice and I still confuse myself a bit so here’s an alternative explanation: