0

I want to make a simple website, with a login, and if the login is a certain string (eg Andrew) I want it to ask for a password with a JS prompt. After the password is typed, an ajax function checks a simple php file for the password.

To understand things better, here is the code:

function login()
            {
                if (document.getElementById("username").value == "" || document.getElementById("username").value == null)
                {
                    alert("Wrong username... actually empty username :)\nTry again.");
                    return;
                }
                if (document.getElementById("username").value == "andrew")
                {
                    var pass = prompt("Password : ", "");
                    if (pass == "")
                    {
                        alert("Your password is empty!\nYou fucked it or you're not really Andrew!");
                        return;
                    } 

                    //THIS IF IS ALWAYS GOING IN ELSE

                    if (check_pass(pass))
                    {
                        alert("good password");
                        return;
                    }
                    else
                    {
                        alert("wrong");
                        return;
                    }
                }
                setCookie("username", document.getElementById("username").value, 365);
                window.location.href = "chat.php";
            }

This is the function that is called after I click the login button.

Here is the check_pass function that I'm using to check if it's the right password (this function works, but it's for reference):

  function check_pass(password)
        {
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    var checker = xmlhttp.responseText;
                    //alert(checker);
                    if (checker.indexOf('1') !== -1)
                    {
                        //alert("in true");
                        return true;
                    }
                    else
                    {
                        //alert("in false");
                        return false;
                    }
                }
            }
            xmlhttp.open("POST", "checkpass.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send("password=" + password);
        }

In the check_pass function, I've put some alerts to test if I actually get the right values, and yes, if the response is 1 it should return true, if it's 0, it should return false. And it does that. But ... in the login function, this part of code:

    if (check_pass(pass))
            {
                alert("good password");
                return;
            }
            else
            {
                alert("wrong");
                return;
            }

Always returns false.

I am doing this since 1 hour now, and I can't see what's wrong. Any ideas ? I've also tried with check_pass(pass) === true and check_pass(pass) == true but it's always returning false.

Any suggestions would be appreciated.

icebox19
  • 493
  • 3
  • 6
  • 15

1 Answers1

2

AJAX is asynchronous -- you'll need a callback function. Modify your check_pass call to take a callback with a result parameter

check_pass(pass, function(result) {
    //do stuff with your result!
}

And modify your AJAX to execute the callback with the data:

function check_pass(password, callback) {
    ....
    xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                var checker = xmlhttp.responseText;
                //alert(checker);
                if (checker.indexOf('1') !== -1)
                {
                    //alert("in true");
                    callback(true);
                }
                else
                {
                    //alert("in false");
                    callback(false);
                }
            }
        }
}
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • I've added the parameter function(result) into the if with check_pass, and also added parameter to check_pass function, and modified return to callback, but ... something is wrong, JS is not handling even the first function. I think I got it, will try again – icebox19 Feb 13 '14 at 16:05