0

I'm trying to get an Ajax function to return true or false (it's a sort of validation function which is called from another function). So the other function needs to get a true or false value from this one so it would know what to do next

This is the function:

function validateLogin( uid, pass, i ){

        var uvalue = uid.value;
        var pvalue = pass.value;

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

        xmlhttp.open("POST","validateCheck.php",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {

                if( xmlhttp.responseText == "Error" )
                    return false;
                else if( xmlhttp.responseText == "Yes" )
                    return a;
                else if( xmlhttp.responseText == 1 )
                        return false;               
                else if( xmlhttp.responseText == 2 )
                        return false;
                else if( xmlhttp.responseText == 3 )
                        return false;
                else if( xmlhttp.responseText == 4 )
                         return false;
                else if( xmlhttp.responseText == 5 )
                        return false;
                else
                    return true;
            }
          }

          xmlhttp.send("uid="+uvalue+"&pass="+pvalue);  
    }

Calling function is:

function validateForm(i){
   var uid = document.myform1.username;
   var pass = document.myform1.password;

   if( validateLogin(uid,pass,i) )
        alert(validateLogin(uid,pass,i));

}

But the function validateLogin is not returning anything to the calling function(validateForm).

Shrey
  • 145
  • 1
  • 7

3 Answers3

1

use callback

function validateLogin(uid, pass, i, callback) {
    var uvalue = uid.value;
    var pvalue = pass.value;

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

    xmlhttp.open("POST", "validateCheck.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange = function ()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {

            if (xmlhttp.responseText == "Error")
                callback(false);
            else if (xmlhttp.responseText == "Yes")
                callback(a);
            else if (xmlhttp.responseText >= 1 && xmlhttp.responseText <= 5)
                callback(true);
            else
                callback(true);
        }
    };

    xmlhttp.send("uid=" + uvalue + "&pass=" + pvalue);
}

and call your function this way

function validateForm(i){
   var uid = document.myform1.username;
   var pass = document.myform1.password;

    validateLogin(uid, pass, i, function (resp) {
        if (resp) {
            alert(resp);
        }
    });
}

using Jquery Deffered Object

function validateLogin(uid, pass, i) {
   var result = jQuery.Deferred();
   ...
    if (xmlhttp.responseText == "Error")
       result.value = false;
    else if (xmlhttp.responseText == "Yes")
       result.value = a;
   ...
   xmlhttp.send("uid=" + uvalue + "&pass=" + pvalue);
   return result.value;
}
yesterdaysfoe
  • 768
  • 3
  • 7
  • 23
  • thanks it worked, how can i return a value from vaidateLogin() function, in case if it is called by another function. Please give some suggestions. – Shrey Jul 29 '15 at 08:05
  • 1
    In that case, you might want to use **jQuery Deferred Object** – yesterdaysfoe Jul 29 '15 at 08:10
0

This function will not alert any thing. As your function execution get completed and still your ajax process work in background and your function does not return any thing and the if condition will get failed.

If you wany any validation then you have to place validation in ajax resonse.

0
  1. you can call it synchronously if you need the result for the next operations:

xmlhttp.open("POST","validateCheck.php",false);

  1. give a callback to the asynchronous function to work with the result

  2. have a look on the jQuery Deferred Object

  3. set a global var or a html element to the return value

...