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
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

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.