Easy! Just use the PHP header function like so. Just be sure to replace the value of success.php
with the actual final location you want users to end up in:
//try to send a message
if(mail(MY_EMAIL, EMAIL_SUBJECT, setMessageBody($fields_req), "From: $email")) {
// echo json_encode(array('message' => 'Your message was successfully submitted.'));
header('Location: success.php');
exit;
} else {
header('HTTP/1.1 500 Internal Server Error');
echo json_encode(array('message' => 'Unexpected error while attempting to send e-mail.'));
}
You can also set the specific HTTP response code by changing this:
header('Location: success.php');
To this:
header('Location: success.php', false, 200);
The 200 response code means “OK” which means the page will be loaded as expected; 100% OK. But you might want to change that to 302 which means the URL has moved temporarilly.