AJAX readystate stuck at 1

Associate
Joined
18 Oct 2002
Posts
1,312
Location
Milton Keynes
Hey all

I am making a self validating AJAX form, which was working fine until I tried to get clever and make it handle multiple fields. It was working fine when it only validated the 'username' field, but when I tried adding support for extra fields, passing variables via a function it ceases to work and gets stuck at readystate == 1, and thats still on the username field without even trying a second

Code:
var url = "reg_val.php?username="; // The server-side script

function handleHttpResponse(whichField) {
	//alert(http.readyState);
	
	//var whichField = "username";
	
	if (http.readyState == 4) {
		if(http.status==200) {
			var results=http.responseText;
			var results_array = results.split("|");
			
			if (whichField == "username") {
				document.getElementById('res_user').innerHTML = results_array[0];
				document.getElementById('username').className = results_array[1];
			}
			else if (whichField == "email") {
				document.getElementById('res_email').innerHTML = results_array[0];
				document.getElementById('email').className = results_array[1];
			}
		}
	}
}

function getHTTPObject() {
	var xmlhttp;

	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if (!xmlhttp){
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	return xmlhttp;	
}

/*  */
function requestUsername(field) {
	document.getElementById('res_user').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	var sId = document.getElementById("username").value;
	http.open("GET", url + escape(sId), true);
	http.onreadystatechange = handleHttpResponse(field);
	http.send(null);
}

function requestEmail(field) {
	document.getElementById('res_email').innerHTML = "<img src=\"images/icons/indicator.gif\">";
	var sId = document.getElementById("email").value;
	http.open("GET", url + escape(sId), true);
	http.onreadystatechange = handleHttpResponse(field);
	http.send(null);
}

var http = getHTTPObject(); // We create the HTTP Object

In the individual fields function, if I change
Code:
handleHttpResponse(field)
to
Code:
handleHttpResponse
and declare the "whichField" variable so its static, then it works, otherwise it gets stuck

obviously I need some way to use the handleHttpResponse function to be able to update different fields
 
Back
Top Bottom