Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts

    ajax function returning the value of responseText?

    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?

    Code:
    function userTaken(){
    var numRows = 99;
    var ajaxUserCheck;
    try{
    	ajaxUserCheck = new XMLHttpRequest();
    } catch (e){
    	try{
    		ajaxUserCheck = new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		try{
    			ajaxUserCheck = new ActiveXObject("Microsoft.XMLHTTP");
    		} catch (e){
    			return false;
    			break;
    		}
    	}
    }
    ajaxUserCheck.onreadystatechange = function(){
    	if(ajaxUserCheck.readyState == 4){
    		return ajaxUserCheck.responseText;
    		//also tried numRows = ajaxUserCheck.responseText;
    	}
    }
    ajaxUserCheck.open("GET", "userexist.php", true);
    ajaxUserCheck.send(null);
    return ajaxUserCheck.onreadystatechange());
    //also tried return numRows
    
    }
    alert (userTaken()); //Get value of function
    Last edited by djh101; 01-24-2011 at 06:04 AM.
    "Yeah science!"
    Online Science Tools

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • Users who have thanked venegal for this post:

    djh101 (01-24-2011)

  • #3
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    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)?
    "Yeah science!"
    Online Science Tools

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • #5
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    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.
    "Yeah science!"
    Online Science Tools


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •