I code simple login / registration functions using jQuery, PHP, and PostgreSQL. The following code is from a PHP file that deals with inputs. It throws an exception when the login / password combination is incorrect.
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
$numResults = pg_num_rows($result);
if ($numResults == 0) {
throw new Exception("Incorrect combination of username and password.");
}
However, on the client side in the jquery, the success function is executed even if the server throws an exception.
$.ajax({
type: "POST",
url:"login.php",
data: dataString,
success: function() {
$('#errorMsg').html('Login is successful!');
$('#errorMsg').show();
$('#usernameTxtBx').val("");
$('#passwordTxtBx').val("");
},
error:function (xhr, ajaxOptions, thrownError){
window.alert(xhr.status);
window.alert(thrownError);
}
});
source
share