30

我已经阅读了所有关于将标题插入 php 表单文件的帖子,以便在提交表单后将用户重定向到另一个 URL - 但我不知道该怎么做。下面是我的代码。你能告诉我在哪里放置标题/重定向,以便信息通过电子邮件发送,然后用户转到另一个 html 页面?

    <?php
    if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "pecraig@moneymovers.com";
    $email_subject = "Mailing List Form";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you
       submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

   // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['company']) ||
        !isset($_POST['street']) ||
        !isset($_POST['city']) ||
        !isset($_POST['state']) ||
        !isset($_POST['zip'])) {
        died('We are sorry, but there appears to be a problem with the form you 
    submitted.');      
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // required
    $company = $_POST['company']; // required
    $street = $_POST['street']; // required
    $city = $_POST['city']; // required
    $state = $_POST['state']; // required
    $zip = $_POST['zip']; // required

    $error_message = "";
    $string_exp = "/^[A-Za-z0-9 .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }  
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$company)) {
    $error_message .= 'The Company you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$street)) {
    $error_message .= 'The Street you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$city)) {
    $error_message .= 'The City you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$state)) {
    $error_message .= 'The State you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$zip)) {
    $error_message .= 'The Zip Code you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Response from Mailing List Page.  Please enter in database.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Company: ".clean_string($company)."\n";
    $email_message .= "Street: ".clean_string($street)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "State: ".clean_string($state)."\n";
    $email_message .= "Zip: ".clean_string($zip)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

?>

<!-- include your own success html here -->

Thanks for subscribing to our mailing list



<?php
}
?>
4

6 回答 6

64

紧接着@mail($email_to, $email_subject, $email_message, $headers);

header('Location: nextpage.php');

请注意,您永远不会看到“感谢订阅我们的邮件列表”

那应该在下一页,如果你回显任何文本,你会得到一个错误,因为标题已经创建,如果你想重定向永远不要返回任何文本,甚至没有空格!

于 2013-06-17T22:10:58.200 回答
12

首先给你的输入类型提交一个名字,像这样name='submitform'

然后把它放在你的php文件中

if (isset($_POST['submitform']))
    {   
    ?>
<script type="text/javascript">
window.location = "http://www.google.com/";
</script>      
    <?php
    }

不要忘记将网址更改为您的网址。

于 2013-09-21T19:21:45.000 回答
8

如果您的重定向使用 PHP,则在重定向指令之前不应向用户回显任何内容。

有关更多信息,请参阅标题

请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP

否则,您可以使用 Javascript 重定向用户。

只需使用

window.location = "http://www.google.com/"
于 2013-06-17T22:13:27.460 回答
3

只要没有 html 和/或文本被打印到标准输出,您就可以在任何地方包含您的标题功能。

更多信息和用法: http: //php.net/manual/en/function.header.php


我在您的代码中看到,echo()如果出现错误或成功,您会输出一些文本。不要那样做:你不能。您只能重定向或显示文本。如果您显示文本,您将无法重定向。

于 2013-06-17T22:10:22.453 回答
0

每当您要重定向时,请发送标头:

header("Location: http://www.example.com/");

不过请记住,在此之前您不能向客户端发送数据。

于 2013-06-17T22:10:24.443 回答
0

一旦遇到这个问题,认为分享我如何解决它是合理的;

我认为在 php 中做到这一点的方法是使用 header 函数:

header ("Location: exampleFile.php");

您可以将该头文件包含在 if 语句中,以便仅在满足特定条件时重定向,如:

if (isset($_POST['submit'])){   header("Location: exampleFile.php")   }

希望有帮助。

于 2018-10-22T22:21:24.237 回答