I am aware there are many questions on this topic already, but having gone through them I am unable to make sense of them in regards to applying to my own contact form probably due to me being rather new at PHP and the fact that so many people use wordpress and contact form 7 so many answers and search results do not help me
I have a PHP contact form I have made over the months to fit my own requirements however I would like the form to redirect to a thank you page on submission
I have tried adding the following code:
header('Location: thank-you.php');
exit;
but I think due to the fact that I have headers at the top of my code just below
include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");
and then some echos in my form code below I am getting the error that headers have already been sent when I submit the form.
I have tried adding the Location with the headers and replacing the echos with it as I have heard this and white-space in the code can cause this..
Below is my code, with the new lines above added:
The top of my code page:
include($_SERVER['DOCUMENT_ROOT']."/includes/header.php");
$name = ($_POST['name']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$from = ($_POST['email']);
$to = 'info@mywebsite.co.uk';
$subject = "Enquiry from Visitor " . $name;
$human = ($_POST['human']);
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
header('Location: thank-you.php');
exit;
This is the code just below the header tag on my form, just before the form starts:
<h2>Contact Us</h2>
<hr width=10% align=left>
<?php
if (isset($_POST['submit']) && $human == '4') {
if (mail ($to, $subject, $message, $headers)) {
echo '<p>Thanks for getting in touch. Your message has been sent & We will get back to you shortly!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
I know I am meant to replace the echos with the Location: thank-you.php but surely I still need the echos for if the person missed a required field or answered the anti-spam question incorrectly?
Note: I have tried replacing
echo '<p>Thanks for getting in touch. Your message has been sent & We will get back to you shortly!</p>';
with
header('Location: thank-you.php');
exit;
but I still get the same error message when I submit the form
I am a newbie to PHP and do not understand how to incorporate a thank you page with my code and whilst keeping the echos for if the form hasn't been filled out correctly
Any help much much appreciated! Thanks :)