1

The return data for this one as I investigated is "authenticated" which means the if statement should take effect. However, to reasons I know not it goes directly to the else part of the statement making the data false even if it's true. Like "authenticated"=="authenticated". It ignores the if part and I don't know why.

function login_admin_user() {

    username = $("#ad-username").val();
    password = $("#ad-password").val();
    $("#button-login").val("Logging in...");

    $.post("ajax-login.php", {
        username: username,
        password: password
    }, function (data) {

        if (data == "authenticated") {
            /*  Execute if authenticated */
            $("#box-login-confirmed").fadeIn("slow", function () {
                $("#button-login").val("Login");
            })
                .delay(1000)
                .fadeOut(400, function () {
                window.location = "home.php";
            });

        } else {

            /*  Execute if invalid login */

            $("#box-login-error").fadeIn("slow", function () {
                $("#button-login").val("Login");
                $("#ad-password").val("");
            })
                .delay(3000)
                .fadeOut(400);

        }

    });
}
karthikr
  • 97,368
  • 26
  • 197
  • 188
John Robertson
  • 1,486
  • 13
  • 16
  • `"authenciated"=="authenticated"` This would be false. You're missing a "t" in the first word. Try simply `if(true)`. Another method of debugging is to `console.log(data)` before the if statement to see what it's actually spitting out at you. – Kodlee Yin Jun 25 '14 at 03:36
  • 1
    How to console.log(data)? – John Robertson Jun 25 '14 at 03:37
  • The result from ajax-login.php is "authenticated" therefore it matches the if condition on the above code. However, even if matches the condition it goes to the else part directly. – John Robertson Jun 25 '14 at 03:40
  • Obviously it doesn't, otherwise you wouldn't be here asking why it's not working. Have you tried the `if(true)`? Also, Right after the line `}, function (data) {` add `console.log(data)` then take a look at this: http://stackoverflow.com/questions/217957/how-to-print-debug-messages-in-the-google-chrome-javascript-console – Kodlee Yin Jun 25 '14 at 03:42
  • I'd suggest you put this before your `if` statement: `console.log("'" + data + "'");` and then look in the debug log so you can see exactly what the `data` variable is. If it's going to the `else` statement, then it's obviously not what you want it to be so you have to figure what value it does have. This should be elementary debugging. Either log the value or set a breakpoint and look at the value. Writing Javascript without knowing basic debugging tools is very, very hard and certainly not recommended. – jfriend00 Jun 25 '14 at 03:45

3 Answers3

1

"authenciated"!="authenticated". You are missing a 't'. Also probably your response may contain spaces. So check what is coming in the response.And its better to trim your response before doing the checking. You can use jquery trim function to remove spaces.

Try like

if($.trim(data) == "authenticated"){
  //Some code
}
else{
  //Some code
}

Why the response contain spaces. You can find the answers here

  1. Space Before Ajax Response (jQuery, PHP)

  2. strange thing, ajax response includes whitespace

  3. Trailing spaces in Ajax response

Community
  • 1
  • 1
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
1

I have encountered that problem as well. Using $.trim() from the response data would help strange whitespaces to be altered.

0

The default return type of the $.post method is HTML. So, if you set the return type as JSON and in your PHP file you return a JSON ENCODED value as "authenticated" .. that might work