问题
I need a simple way to redirect, after a form submits, to a thank you page.
PHP:
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "email@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
My HTML, which I do not want changed:
<form action="mail.php" method="POST" autocomplete="on"></br>
<p>Name:</p><input type="text" name="Name" size="20"></br>
<p>Email:</p><input type="text" name="email" size="20"></br>
<p>Message:</p>
<textarea id="styled" type="textarea" name="message" form="input></textarea></br>
<pre><input type="submit" value="Send"><input type="reset" value="Clear"></pre>
</form>
回答1:
You can use below code. You can use a simple PHP script to redirect a user from the page they entered to a different web page. use header.
<?php
//your code goes here -> mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
?>
回答2:
You could try header("location: next_page.php"); note that nothing can be printed to the browser before this though.
回答3:
use header
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location : path_to_thank_you_page');
exit();
回答4:
Add a header towards the end of mail.php to redirect you to the thank you page. Example
header("Location: thankyou.php");
回答5:
Use header(Location:"yorpage".php);
give relative path to the file.do not add any php statement before header statement.
回答6:
You could rename your index.html in index.php. Then include mail.php in index.php. After form submit redirect on index.php
<?php include('mail.php'); ?>
<form action="<?= $_SERVER['PHP_SELF'];?>" method="POST" autocomplete="on"></br>
来源:https://stackoverflow.com/questions/14810399/php-form-redirect