This question already has an answer here:
- Page redirect with successful Ajax request 10 answers
I have a login page. When the user enters username, password, and click the LogIn button, the button calls an Ajax XmlHttpRequest, which send the username and password to the process.php. The php process the data, and return if username or password is not correct. But when correct, I want to redirect the user to the session.php. Php header doesn't work because it takes the page code, and JavaScript doesn't work too, because doesn't run the code. How can I do it?
Here is the Ajax:
function logIn() {
var loader = document.getElementById('loaderParent');
var form = document.getElementById('formI');
form.style.display = "none";
loader.style.display = "block";
setTimeout(function() {
var xhttp = new XMLHttpRequest();
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// console.log(4);
document.getElementById("form").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "class/process.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("zone=LogIn§ion=2&username="+username+"&password="+password);
}, 500);
}
</div>