IMNOboist Posted July 12, 2007 Share Posted July 12, 2007 I have some AJAX code retrieving a result from a PHP script. Sometimes it works, sometimes it doesn't. I'm wondering if the script just gets ahead of itself somehow... I don't know. Here's the code: function ajax() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } return xmlHttp; } function returnAjax(getVars) { xmlHttp = ajax(); xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById('returnVals').value = xmlHttp.responseText; } } strURL = "process.php?" + getVars; xmlHttp.open("GET",strURL,true); xmlHttp.send(null); } function checkSN(strSN,strDis) { returnAjax('action='+strDis+'&strSN='+strSN); if(document.getElementById('returnVals').value == 'true') { alert("This serial number already exists!"); } document.getElementById('returnVals').value = ''; } What happens is the user pushes a the submit button which triggers the "checkSN" function. I keep entering the same serial number (which already exists). Sometimes, the alert is triggered. Sometimes it isn't. What's the deal? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
IMNOboist Posted July 13, 2007 Author Share Posted July 13, 2007 I figured it out. The script IS getting ahead of itself, so to speak. I guess Javascript just keeps going after the AJAX call. What I did was create a new function that checked the value of 'returnVals' and set a timeout on that function of 500. That way, the AJAX has time to get the response into the returnVals variable so when it gets checked, it knows what to do. Here's the resulting code: function ajax() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } return xmlHttp; } function returnAjax(getVars) { xmlHttp = ajax(); xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById('returnVals').value = xmlHttp.responseText; } } strURL = "process.php?" + getVars; xmlHttp.open("GET",strURL,true); xmlHttp.send(null); } function checkSN(strSN,strDis) { returnAjax('action='+strDis+'&strSN='+strSN) setTimeout('checkSNReturn()',500); } function checkSNReturn() { if(document.getElementById('hidVals').value == 'true') { alert("Serial number already exists in Bound Book!"); } document.getElementById('hidVals').innerHTML = ''; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.