I'm running some ajax on my website to handle form submission, However, For some reason whenever I check the response from PHP, it ignores my handling and goes straight to the bottom.
//Login Form Submission
$('form.loginform').on('submit', function (e) {
e.preventDefault();0
//Grab Data
var that = $(this),
url = that.attr('action'),
method = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: method,
data: data,
success: function (response) {
console.log("Ajax Response: \"" + response + "\"");
console.log(typeof response);
if(response === "0"){
document.getElementById('error_message').innerHTML = "Incorrect password";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
}else if(response === "1"){
window.location.href = "./index.php";
}else if(response === "2"){
document.getElementById('error_message').innerHTML = "Please fill all required fields.";
document.getElementById('error_message').style.color = 'red';
} else if(response === "3"){
document.getElementById('error_message').innerHTML = "That username does not exist. Please try again.";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
} else if(response === "4"){
document.getElementById('error_message').innerHTML = "Your account has been banned on this website. Please contact an administrator.";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
} else {
document.getElementById('error_message').innerHTML = "Error while logging in. Please try again.<br />If you continue to recieve this error, Please contact an administrator.";
document.getElementById('error_message').style.color = 'red';
document.getElementById('error_message').style.borderColor = 'red';
}
//This is the output from the php script
}
});
return false;
});
That is my current code, The output I get is
Ajax Response: "1"
String
However, It then goes straight to
'Error while logging in, Please contact an Administrator'
My question is Why is it ignoring the 'if' statements above the 'else', even though the response is '1'?
Try response.trim() == "1"
since log shows its 1 and String so may be some white space is the issue.