表单提交后PHP重定向到另一个页面 [英] PHP Redirect to another page after form submit

查看:43
本文介绍了表单提交后PHP重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 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?

    <?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.

";

    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)."
";
    $email_message .= "Last Name: ".clean_string($last_name)."
";
    $email_message .= "Email: ".clean_string($email_from)."
";
    $email_message .= "Telephone: ".clean_string($telephone)."
";
    $email_message .= "Company: ".clean_string($company)."
";
    $email_message .= "Street: ".clean_string($street)."
";
    $email_message .= "City: ".clean_string($city)."
";
    $email_message .= "State: ".clean_string($state)."
";
    $email_message .= "Zip: ".clean_string($zip)."
";


// create email headers
$headers = 'From: '.$email_from."
".
'Reply-To: '.$email_from."
" .
'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);

Right after @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!

这篇关于表单提交后PHP重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆