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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.