tags:

views:

254

answers:

1

Im making an ajax login form. When a user logs in with the wrong password login.php will return "Incorrect password". But javascript doesn't seem to be picking it up.

$('#login_btn').click(function(){
      var username = $('#login_username').val();
      var password = $('#login_password').val();
      $.post("login.php", { username: username, password: password },
       function(data){
        if(data == 'Incorrect password'){
         $('#login_callback').html(data); 
        }
        else{
         $('#login').html(data);
        }
       }
      );        

     });

I can login fine, but its just when its the wrong password.

+3  A: 

Try changing your request to use $.ajax instead of $.post since $.post silently fails.

$.ajax({
   type: "POST",
   url: "login.php",
   dataType: 'text',
   data: { username: username, password: password },
   success: function(data) {
     if(data == 'Incorrect password'){
       $('#login_callback').html(data);        
     }
     else{
       $('#login').html(data);
     }
   },
   error: function(msg) {
    alert("O NOES");
   }
});

This is based on my theory that login.php returns a non-200 response on failure. Just a hunch though.

seth
+1 alert("O NOES");
Jason