I have just set up a PHP contact form using the very helpful tutorial from kirupa (thanks).
However instead of an echo stating “Data has been submitted to $to!” I wanted it to redirect to a thank you page. I searched the kirupa forums but found the best solution from a tutorial by skter4938 elsewhere.
Any way the code on the kirupa tutorial is:
<?php
if(isset($_POST['submit'])) {
$to = "you@you.com";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field
E-Mail: $email_field
Message:
$message";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";}
?>
Change it to (taken from skter4938):
<?php
if(isset($_POST['submit'])) {
$to = "you@you.com";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field
E-Mail: $email_field
Message:
$message";
$success = mail($to, $subject, $body);
}
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=YOUR_PAGE_HERE.html\">";
} else {
echo "Sorry error please try again...";
}
?>
Remove the echo line and add $success = in front of mail etc.
Then add a new line with:
if ($success){
print “<meta http-equiv=“refresh” content=“0;URL=YOUR_PAGE_HERE.html”>”;
This will redirect to your page after the user clicks the submit/send button. I’m no expert so I cant go in to the details for you but it worked for me. nice and simple.
You will need to do the same for the error message as well if you want a redirected page for that.
I hope it helps someone (although I did not write this code so take no credit).
PP