Hey there, I'm attempting to implement a redirect upon receiving a response from an ajax request. However, it seems that the windows.href.location doesn't execute the redirect until the PHP background process finishes processing. Check out my code below:
// This is the PHP script running in the background
<?php
$background = new BackgroundProcess('email.php');
$background->run(); // The background process is currently running
return $background->getPid(); // This immediately returns to the frontend
?>
Here's my ajax request using jQuery:
$.ajax({
url: '/payment',
data: form.serialize(),
dataType: 'json',
success: function(txt){
if (txt.response == "ok"){
console.log("Redirecting now");
window.location.href = "/confirmation";
}else{
alert("Failed process");
}
},
type: 'POST'
});
The console prints "Redirecting now", indicating that a response has been received. But the expectation is for the next line, which contains window.href, to be executed immediately. However, this doesn't happen until the PHP script finishes its background process. Any ideas on how to resolve this?
Note: The PHP process itself is operating as intended; it's just that the JavaScript redirect isn't executing promptly and is waiting for the PHP script to complete.