Jump to content

[SOLVED] Why won't this work?


therealwesfoster

Recommended Posts

I'm checking on the fly whether or not the username is valid and available. The PHP file is working, and the Ajax is working.. here is my code:

 

function checkUsername(obj)
{
var pattern = new RegExp("^[a-zA-z0-9_]{3,20}$","i");
var error = 0;

if ( !obj.value.match(pattern) )
{
	error = 1;
}

if ( obj.value == "" )
{
	error = 1;
}


// Setup ajax stuff
ajax = GetXmlHttpObject();
if (ajax==null)
{
	alert ("Your browser does not support AJAX!");
	return;
}

var url = "./includes/ajaxcheck.php?username="+obj.value;
ajax.onreadystatechange=function()
{
	if (ajax.readyState == 4)
	{
		var res = ajax.responseText;

		alert("Response:"+res+"\nError: "+error+""); // debugging purposes

		if (res == "1")
		{
			error = 1;
		}
	}
}

ajax.open("GET",url,true);
ajax.send(null);

if (error == 0)
{
	// DO ERROR
}
else
{
	// DONT DO ERROR
}

return error;
}

 

The alert I have setup returns "Response: 1  Error: 0".

 

If the response-text from the PHP file is 1, i want to throw an error. But if it's 0, I want it to be ok. But it's not throwing an error either way I do it.

 

Whats the problem? (should be around this line)

		var res = ajax.responseText;

		alert("Response:"+res+"\nError: "+error+""); // debugging purposes

		if (res == "1")
		{
			error = 1;
		}

Link to comment
https://forums.phpfreaks.com/topic/84177-solved-why-wont-this-work/
Share on other sites

Awesome.. it works :)

 

But what is the 3rd argument for? (the true/false)

 

I can't find a clear answer with google

 

glad to hear you solved but the correct answer is still not there with me.What it does and why is written everywhere but in certain condition it works and in certain it doesn't not.I myself is also doing R&D and will definitely let you know the reason .

 

have a great day

its for the Asynchronous call, if its true the script will wait till it gets the response then continue with the other code, else it will execute it and immediately continue with the code, I think that was it... been a long time tho

 

Alright thanks :)

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.