Users can contact us through a Bootstrap popup modal. Currently, it submits everything and echoes a success message (still in the popup modal format). I'd like, instead of echoing the message, to redirect to an entirely new page (not another popup).
I've tried using "header('Location: contact-thank-you.html'); exit();", which seems the obvious solution. However, this is just treated as another echo - it prints out the entire page (HTML and all) in the popup modal.
There are no error messages in the console.
There are no spaces above the start of the php file (which I understand can cause issues).
I'm able to solve this by redirecting via javascript, but I want to understand why this doesn't work. I'm not echoing anything else earlier on the page (although I do have an error if someone doesn't submit a required piece of info), so shouldn't header() work? Does it just not work well with Bootstrap?
The site is built in Jekyll, if that has any bearing on the issue.
Here's a rough outline of my php, with a lot of extra stuff removed for your viewing pleasure :) :
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
// Require Email
if (empty($email)) {
http_response_code(400);
exit;
}
// Set the email subject.
$subject = "$name - New Contact";
// Build the email content.
$email_content = "";
$email_content .= "Name: $name\n";
$email_content .= "Email: $email\n";
$email_headers = "From: $name <$email>";
$recipient = "myname@mycompany.com";
send_mail($recipient, $subject, $email_content, $email_headers);
// confirm message was sent
header('Location: contact-thank-you.html');
exit();
}
// removed mailer function and other sections unessential to this question
?>