简体   繁体   English

表单提交后PHP重定向到另一个页面

[英]PHP Redirect to another page after form submit

I have read all your posts about inserting headers into a php form file in order to redirect the user to another URL AFTER the form is submitted - but I can't figure out how to do it.我已经阅读了您关于将标题插入 php 表单文件以便在提交表单后将用户重定向到另一个 URL 的所有帖子 - 但我不知道该怎么做。 Below is my code.下面是我的代码。 Can you show me where to put the header/redirect so that the information gets e-mailed and then the user goes to another html page?你能告诉我把标题/重定向放在哪里,以便信息通过电子邮件发送,然后用户转到另一个 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
}
?>

Right after @mail($email_to, $email_subject, $email_message, $headers); @mail($email_to, $email_subject, $email_message, $headers);之后@mail($email_to, $email_subject, $email_message, $headers);

header('Location: nextpage.php');

Note that you will never see 'Thanks for subscribing to our mailing list' 请注意,您将永远不会看到“感谢您订阅我们的邮件列表”

That should be on the next page, if you echo any text you will get an error because the headers would have been already created, if you want to redirect never return any text, not even a space! 那应该在下一页上,如果您回显任何文本,您将得到一个错误,因为标头已经被创建,如果您要重定向,则永远不会返回任何文本,甚至也不是空格!

If your redirect is in PHP, nothing should be echoed to the user before the redirect instruction. 如果您的重定向是用PHP进行的,则在重定向指令之前不应向用户回显任何内容。

See header for more info. 有关更多信息,请参见标题

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP 请记住,在发送任何实际输出之前,必须先通过常规HTML标记,文件中的空白行或从PHP调用header()

Otherwise, you can use Javascript to redirect the user. 否则,您可以使用Javascript重定向用户。

Just use 只需使用

window.location = "http://www.google.com/"

First give your input type submit a name, like this name='submitform' . 首先给您的输入类型提交一个名称,例如name='submitform'

and then put this in your php file 然后将其放入您的php文件中

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

Don't forget to change the url to yours. 不要忘记将网址更改为您的网址。

You can include your header function wherever you like, as long as NO html and/or text has been printed to standard out. 只要没有将html和/或文本打印为标准输出,就可以在任意位置包含标头函数。

For more information and usage: http://php.net/manual/en/function.header.php 有关更多信息和用法,请访问: http : //php.net/manual/en/function.header.php


I see in your code that you echo() out some text in case of error or success. 我在您的代码中看到,在错误或成功的情况下,您可以echo()出一些文本。 Don't do that: you can't. 不要那样做:你不能。 You can only redirect OR show the text. 您只能重定向或显示文本。 If you show the text you'll then fail to redirect. 如果显示文本,则将无法重定向。

Whenever you want to redirect, send the headers: 每当您要重定向时,发送标头:

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

Remember you cant send data to the client before that, though. 但是请记住,在此之前您无法将数据发送到客户端。

Once had this issue, thought it reasonable to share how I resolved it; 一旦遇到这个问题,就认为分享我的解决方案是合理的;

I think the way to do that in php is to use the header function as: 我认为在php中执行此操作的方法是将标头函数用作:

header ("Location: exampleFile.php");

You could just enclose that header file in an if statement so that it redirects only when a certain condition is met, as in: 您可以仅将该头文件包含在if语句中,以便仅在满足特定条件时才重定向,例如:

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

Hope that helps. 希望能有所帮助。

{ ?> <script type="text/javascript"> window.location = "https://yourdomainhere/thank-you/"; </script> <?php} { ?> <script type="text/javascript"> window.location = "https://yourdomainhere/thank-you/"; </script> <?php} worked for me when placed after if ( isset( $_POST['form-submitted'] ) ) { . { ?> <script type="text/javascript"> window.location = "https://yourdomainhere/thank-you/"; </script> <?php}if ( isset( $_POST['form-submitted'] ) ) {之后放置时对我if ( isset( $_POST['form-submitted'] ) ) { Thank you Aria for the help!谢谢阿丽亚的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM