Alain Elemia
Alain Elemia

Reputation: 108

Ajax Success Function Not Working Properly

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..

Upvotes: 1

Views: 547

Answers (3)

Amit Chauhan
Amit Chauhan

Reputation: 682

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

Upvotes: 0

Rahul Patel
Rahul Patel

Reputation: 639

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

Upvotes: 0

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

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.

Upvotes: 0

Related Questions