I'm making a login form true an XMLHttpRequest. When a user enters incorrect details, a message will show up with an error. But when a user enters correct details, he should go to a new page (dashboard).
This is a part of my PHP
<?PHP
// Details are correct
$output = header('Location:dashboard/index.php');
// Details are incorrect
$output = '<h6 class="omessagetext" id="error" style="color: #f05461">Incorrect e-mail or password</h6>';
?>
This is my javascript
var data = new FormData();
data.append("email", document.getElementById("inputmail").value);
data.append("password", document.getElementById("inputpassword").value);
var xhr = new XMLHttpRequest();
xhr.open("POST", "login.php");
xhr.onload = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var responsetext = xhr.responseText;
if (responsetext.location){
window.location.href = responsetext.location
}else {
document.getElementById('errordiv').innerHTML = responsetext;
}
}
};
xhr.send(data);
return false;
I've searched a lot but can't find the answer. Hope you can help me!