-2

Good day, I'm trying to make a login screen in php & mysql with Jquery(ajax). but I'm having a problem with if else statement.

Here's my code.

    $("#btnLogin").click(function(){
            username=$("#textUsername").val();
            password=$("#textPassword").val();

            $.ajax({
                    type: "POST",
                    url: "login.php",
                    data: "username="+username+"&password="+password,
                    success: function(result){

                            var res = result;
                            if(res == 'correct'){
                                    alert("success login");
                            } else {
                                    console.log("fail login");
                            }
                    }
            });
             return false;
    });

it always fall in else. even the result is return as correct

Lester1992
  • 493
  • 2
  • 9
  • 20

3 Answers3

0

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. Trim the response $.trim (result) and it'll work.

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
John Robertson
  • 1,486
  • 13
  • 16
  • @Lestr1992 glad to help bro! Accept my answer to let others know this is done and we can move forward – John Robertson Aug 08 '14 at 08:46
  • 1
    This should have been flagged as a duplicate. Your answer was posted before OP had even clarified what the returned `result` was. Answers on StackOverflow should never start with the word "Probably". – James Donnelly Aug 08 '14 at 08:46
  • but actually "var res = $.trim(result);" this what i did. but still thank you dude :) – Lester1992 Aug 08 '14 at 08:46
  • Just helped the guy solved his problem then someone just downvoted. What a total nonsense. – John Robertson Aug 08 '14 at 08:49
  • @JamesDonnelly I know the answer because I had this problem before. – John Robertson Aug 08 '14 at 08:50
  • @JohnRobertson again, you answered before OP had clarified what the returned `result` was. Without knowing that, this problem could have been caused by anything: a PHP error, a JavaScript error, a typo, a missing jQuery library, etc. Now that we do know what the returned `result` is, we can flag this question as a duplicate. – James Donnelly Aug 08 '14 at 08:55
  • @JamesDonnelly And by the way. I answered with probably because there could have been a "PHP error, a JavaScript error, a typo, a missing jQuery library, etc." as what you said. What a nonsense. – John Robertson Aug 08 '14 at 09:00
  • 1
    I think what people are trying to say is that if the question cannot be answered, it is better to request additional information than to post a "shot in the dark" answer. Now it turns out that this shot in the dark actually solved the problem, so @Lestr1992 should upvote and accept this as the correct answer. And please don't down vote posts just because they answer a duplicate question. Instead, close the original question as a duplicate and leave the answers alone. – Lundin Aug 08 '14 at 09:41
0
$(document).ready(function(){
$("#add_err").css('display', 'none', 'important');
 $("#login").click(function(){  
      username=$("#name").val();
      password=$("#word").val();
      $.ajax({
       type: "POST",
       url: "login.php",
        data: "name="+username+"&pwd="+password,
       success: function(html){    
        if(html=='true')    {
         //$("#add_err").html("right username or password");
         window.location="dashboard.php";
        }
        else    {
        $("#add_err").css('display', 'inline', 'important');
         $("#add_err").html("<img src='images/alert.png' />Wrong username or password");
        }
       },
       beforeSend:function()
       {
        $("#add_err").css('display', 'inline', 'important');
        $("#add_err").html("<img src='images/ajax-loader.gif' /> Loading...")
       }
      });
    return false;
});

});

-1

You should trim before comparison

     if($.trim(res) == 'correct'){


       }
Vicky
  • 603
  • 5
  • 6