Reputation: 321
Am trying to redirect to a page based on my response. But am doing it wrong. Tried the answers Here But not working. my Ajax script is below.
success: function (response) {
if (response === 'success') {
window.location.replace("home");
} else {
$(".error_message").fadeIn();
$(".error_message").html("");
$(".error_message").html(response);
$(".error_message").fadeOut(5000);
}
}
My php code is below
if ($stmt->execute()) {
echo "success";
}
$stmt->close();
It echoes the message after the form was submitted successfully.
But i want it to reload not getting stuck at the same page.
Upvotes: 0
Views: 38
Reputation: 68
Try the following:
AJAX:
success: function (response) {
if (response.success) {
alert(response);
location.reload();
} else {
$(".error_message").fadeIn();
$(".error_message").html("");
$(".error_message").html(response);
$(".error_message").fadeOut(5000);
}
}
PHP:
if ($stmt->execute()) {
return json_encode(array("success" => true));
}
$stmt->close();
I changed the PHP so that it's sending a JSON response instead of plain text, along with it being in an array that has a success field which will be checked by the JavaScript to see if it's true or false.
Upvotes: 1