1

I have been trying to solve this problem whole day,but it seems I am not able.

Here is my code:

function mailcheckajax(email){
    //var add = $('#mail').val();
    //window.location = "index.php?address="+add;
    //alert(window.location.pathname);
    var loc = window.location.pathname;
    var dir = loc.substring(0, loc.lastIndexOf('/'));
    var xmlhttp;

    // code for IE7+, Firefox, Chrome, Opera, Safari
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            if(xmlhttp.responseText != "valid"){
                window.mailerror = "true";
            } else {
                window.mailerror="false";
            }
        }
    }

    xmlhttp.open("GET","/application/views/client/mailvalidate.php?address="+$('#'+email).val(),true);
    xmlhttp.send();
    return "true";
}

It calls this function and comes another line of code:

mailcheckajax('studentEmail');

if (checkvalidate == "true") {
    if (validateStep(oldstep) == false) {
        $("html, body").animate({
            scrollTop: 0
        }, "slow");
        return false;
    }
}

I need that on onclick() firstly was executed mailcheckajax function fully in order to get response from server using ajax and only after that other code.Or do not exit from function before it gets ajax response.

Can anyone help?

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
Nika Tsogiaidze
  • 949
  • 14
  • 18
  • why don't you use the jquery ajax function and include your code inside the complete callback? u are using jquery anyway.. way easier to use .ajax() or .get() – gulty Aug 20 '14 at 13:14

1 Answers1

1

You should add the other code you need to execute after the response to another function and call that function inside the handler:

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        if(xmlhttp.responseText != "valid"){
            window.mailerror = "true";
            //CALL HERE THE FUNCTION YOU WANT TO EXECUTE AFTER THE RESPONSE IS RECEIVED
        } else {
            window.mailerror="false";
        }
    }
}

Hopte that helps!

Ruben R Aparicio
  • 663
  • 4
  • 10