View Full Version : ajax function returning the value of responseText?

01-24-2011, 06:48 AM
I want my ajax function to return the value it receives from my PHP file (right now all my PHP file does is echo a digit). If I replace return ajaxUserCheck.responceText with an alert function, the value will pop up, but I can't get it from the onreadystatechange function to the numRows variable or to the return value of the whole (userTaken) function. Any advice?

function userTaken(){
var numRows = 99;
var ajaxUserCheck;
ajaxUserCheck = new XMLHttpRequest();
} catch (e){
ajaxUserCheck = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
ajaxUserCheck = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
return false;
ajaxUserCheck.onreadystatechange = function(){
if(ajaxUserCheck.readyState == 4){
return ajaxUserCheck.responseText;
//also tried numRows = ajaxUserCheck.responseText;
ajaxUserCheck.open("GET", "userexist.php", true);
return ajaxUserCheck.onreadystatechange());
//also tried return numRows

alert (userTaken()); //Get value of function

01-24-2011, 07:19 AM
Your approach is fundamentally flawed. The reason it will never work is that taking an asynchronous trip to the server takes time, and your function won't wait. It starts the request, but then immediately continues execution, so it won't be able to return anything that has to do with the request.

The right way to do this is to trigger anything that relies on the request to have already finished from within the onreadystatechange handler.

01-24-2011, 07:47 AM
I'm doing a registration form, I have a three if/elseif/else statement that determines:
a) input text is at least 3 characters
b) username does not exist (determined by the ajax)
c) if none of the above, outputs successful

So what would you suggest? Delay this function until onreadystate returns a value (not sure how to delay it or how to retrieve the returned value)? Replace the function with the ajax function and place the statement inside onreadystate (which is still threatened by the delay problem, since there's more functioning that determines whether or not there are any errors in any fields)?

01-24-2011, 08:19 AM
The way this sort of client side validation is usually done is to validate each input separately as soon as it loses focus. I.e., when the user tabs away from the input text, it's checked for being at least 3 characters, and if not, an error is shown (not in an alertbox, though; something unobstrusive like changing the color of the input or adding some text below the input).

If the user tabs away from the username field, the ajax request is started, and when it comes back, it's evaluated, and if the username is already taken, again an error is shown.

This way errors can be shown immediately, so the ajax request has some time to come back before the user actually submits the form.

Of course it's entirely possible that the user submits the form before validation has come back from the server. But that's really a non-issue, because client side validation is supposed to provide a better user experience, not to prevent a user from actually submitting. You do the real validation on the server side, after the form has been submitted, anyway.

01-24-2011, 08:39 AM
I have two functions, one that checks the input type on text change, checks for errors, and then ads a message in the HTML and gives a variable for the submit function. The submit function checks the variables and prompts a message if there are any problems on submit. And of course, I have a php check for noscript users and for the actual check after submission. Anyway, now that I know what the problem is, I'm sure I can find a way to fix it. Later, though, I've got essays to write. Thanks for your help.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum