1

Ok so my problem is my Ajax Success Function is not working properly but IS WORKING properly on another web page. Annoying right? They have the same code(copy pasted it and edited) with different variables of course.

Working Code:

      $.ajax({
       type: 'POST',
       url: 'login.php',
        data: {
                uname: username,
                pass: password
              },
       success: function(html){    

      alert(html);
        if(html=='true')    {
         $("#add_err").html("right username or password");
         window.location="../home.php";
        }
        else    {
        $("#add_err").css('display', 'inline', 'important');
         $("#add_err").html("<img src='images/alert.png' />Wrong username or password"+username+password);

        }
       },

       beforeSend:function()
       {
        $("#add_err").css('display', 'inline', 'important');
        $("#add_err").html("<img src='images/ajax-loader.gif' /> Loading...")
       }
      });

Login.php will just echo the word "true" or "false" depending on the query.

This is the Not Working Code:

 $.ajax({
       type: 'POST',
       url: 'loginphp.php',
        data: {
                username1: username,
                password1: password
              },

       success: function(html){    

      alert(html);
        if(html=='true')
        {
            $("#errorlogin").html("right username or password");
            window.location="../php/home.php";
        }
        else 
        {
            $("#errorlogin").css('display', 'inline', 'important');
            $("#errorlogin").html("<img src='images/alert.png' />Wrong username or password"+username+password);
        }           

        },



       beforeSend:function()
       {
        $("#errorlogin").css('display', 'inline', 'important');
        $("#errorlogin").html("<img src='images/ajax-loader.gif' /> Loading...")
       }
      });

Loginphp.php will just echo the word "true" or "false" depending on the query.

as you can see they are technically the same but the not working code, what happens is even when the loginphp.php echoed TRUE, success function will go directly to ELSE.

I know it really echoed TRUE because I used "alert(html)" and it really shows TRUE.

But in the working code, the "if(html=='true')" is triggered. I don't know what is happening..

Alain Elemia
  • 108
  • 7
  • String comparisons are case sensitive. Is loginphp.php returning "true" or "TRUE" or some other variation? – Chris Bergin Mar 18 '16 at 03:26
  • it is returning a string which is exactly "true". I already fixed it but I didn't get to know the real problem. I just used the working code and substitute the variables.. can you tell me what will I do about this question? is there like a answered already button? – Alain Elemia Mar 19 '16 at 11:46

3 Answers3

0

You should unquote to the true:

if(html==true) //or just, if(html)

Otherwise it will look for string "true" and this is why it goes to the else part.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

You can use if(html){} which will run for any value of html except for false.

RredCat
  • 5,259
  • 5
  • 60
  • 100
Rahul Patel
  • 639
  • 6
  • 12
0

try on login.php at end

echo json_encode(array("msg"=>"true"));

or on success you can check like this

if(html.msg == "true")
{}

try it may be help

Amit Chauhan
  • 682
  • 7
  • 22