Jump to content

[SOLVED] AJAX code sometimes works, sometimes doesn't. Help?


IMNOboist

Recommended Posts

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!

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 = '';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.