therealwesfoster Posted January 2, 2008 Share Posted January 2, 2008 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; } Quote Link to comment Share on other sites More sharing options...
priti Posted January 3, 2008 Share Posted January 3, 2008 hi, kindly change ajax.open("GET",url,true); this with ajax.open("GET",url,false); regards Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted January 3, 2008 Author Share Posted January 3, 2008 Awesome.. it works But what is the 3rd argument for? (the true/false) I can't find a clear answer with google Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 3, 2008 Share Posted January 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
priti Posted January 3, 2008 Share Posted January 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
therealwesfoster Posted January 3, 2008 Author Share Posted January 3, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.