JQuery Ajax - Callback

Im creating an ajax registration form. When the user logs in with the wrong password, login.php will return "Invalid password". But javascript doesn't seem to pick 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 in order, but its easy when it has the wrong password.

+4
source share
1 answer

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

 $.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 error. Just guess.

+9
source

All Articles