PHP Form Redirect [duplicate]

半城伤御伤魂 提交于 2021-02-04 21:56:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!