0

I have this Javascript/AJAX function:

function submitValidate() {
    var xmlhttp;
    xmlhttp = null;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        try {  xmlhttp = new XMLHttpRequest();
            } 
        catch (e){}
    } else if (window.ActiveXObject) { 
        try {  xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
            } 
        catch (e){
            try {  xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); // code for IE6, IE5
                } 
            catch (e){}
        }
    }

    if (xmlhttp) {   
        xmlhttp.open("GET","registerTest.php",true);
        xmlhttp.send();

        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                if (xmlhttp.responseText==="true") {
                    return true;
                } 
            } // if xmlhttp.readyState && xmlhttp.status
        }// xmlhttp.onreadystatechange
    }// if

    return false;   
} //submitValidate()

I would like for submitValidate() to return true or false.

However I have come to realize that I cannot just 'return true;' inside the onreadystatechange function as I did here -

    xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                if (xmlhttp.responseText==="true") {
                    return true;
                } 
            } // if xmlhttp.readyState && xmlhttp.status
        }// xmlhttp.onreadystatechange

.. if I want submitValidate() to return true in that case.

Can somebody please tell me how I can get submitValidate() to return true if the onreadystatechange function returns true?

Thanks in advance.

nOrakat
  • 73
  • 11
  • 1
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – JJJ May 04 '14 at 09:12
  • Definitely a duplicate. It's ASYNCHRONOUS. Rather use an event-based pattern to solve these kinds of problems. You'll only do yourself a favor. – fjc May 04 '14 at 09:23
  • Not sure what you meant by 'event based pattern'.. but thanks for the replies. It's a very short PHP script so maybe it is ok to keep it synchronous. The file will always be there.. – nOrakat May 04 '14 at 15:44

1 Answers1

0

Basically you cannot. There are two possibilities for you:

  1. Send a request with parameter async=false and your code will be blocked until the request is not done. This is not user frienldy.

    var myReturnValue;
    xmlhttp.open("GET","registerTest.php",false);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            if (xmlhttp.responseText==="true") {
                myReturnValue = true;
            } 
        } // if xmlhttp.readyState && xmlhttp.status
    }// xmlhttp.onreadystatechange
    xmlhttp.send();
    alert(myReturnValue);
    
  2. Keep the things as they was, your code will continue to execute while xmlhttp.onreadystatechange will be processed parallelly once the request is done (we can't be sure exactly when this occurs, due to network bandwidth and etc..)

Hrvoje Golcic
  • 3,446
  • 1
  • 29
  • 33