【问题标题】:php contact form with reply message带有回复消息的php联系表
【发布时间】:2011-09-23 19:55:51
【问题描述】:

我创建了一个联系表单,它使用我的 php 脚本通过电子邮件发送表单参数:

//File email.php
<?php
include('variables.php');
$to = "info@timeshare.org.uk";
$subject = "Quote From Website";

$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$resort = $_POST["resort"];
$headers = "From: ".$to."\r\n";

$message = "Name: ".$name."\r\n".
    "Email: ".$email."\r\n". 
    "Number: ".$number."\r\n".
    "Resort Name:".$resort."\r\n";

if (mail($to,$subject,$message,$headers)){
    $replyHeader = "Thank You!";
    $replyMessage = "Thank you for using our quick contact form. We will get back to you as soon as possible.";
}else{
    $replyHeader = "Oops!";
    $replyMessage = "An Error Occured whilst trying to send the form. Please try again later";
}
header( 'Location: http://localhost/TimeShare/formReturn.php' ) ;
?>

在提交表单时指向此代码。

现在,如果邮件功能成功,我希望表单重定向到 formReturn.php,但是我希望特定 div 中的内容显示 $replyHeader 和 $replyMessage。

因此,一旦表单发布,它会重定向到显示成功消息或错误消息的页面。

现在在我的 formReturn.php 页面上,我尝试包含变量,如下所示:

//File: formReturn.php
<body>
    //Header code...
    //sidebar code...
    <div>
        <?php include('php/email.php'); ?>
        <h1> <?php echo $replyHeader; ?> </h1>
        <p> <?php echo $replyMessage ?> </p>
    </div>
    //Footer code...
</body>

此代码的问题是,因为我包含 email.php 文件,我最终会出现重定向循环。哪个不好!

那么我如何根据 mail() 函数的成功或失败将变量 $replyHeader 和 $replyMessage 放到该特定位置的页面上

【问题讨论】:

    标签: php


    【解决方案1】:

    您还可以在第二页中添加 GET 变量:

    if($mail_is_succesfull)
       header('Location: http://localhost/TimeShare/formReturn.php?success=true');
    else
       header('Location: http://localhost/TimeShare/formReturn.php?success=false') ;
    

    然后您可以在其他页面中添加消息。

    【讨论】:

      【解决方案2】:

      你可以将它作为一个 get 变量传递

      <?php
      
      include('variables.php');
      
      $to      = "info@timeshare.org.uk";
      $subject = "Quote From Website";
      $name    = $_POST["name"];
      $email   = $_POST["email"];
      $number  = $_POST["number"];
      $resort  = $_POST["resort"];
      $headers = "From: ".$to."\r\n";
      
      $message =  "Name: "      . $name   . "\r\n".
                  "Email: "     . $email  . "\r\n". 
                  "Number: "    . $number . "\r\n".
                  "Resort Name:". $resort . "\r\n";
      
      if (mail($to,$subject,$message,$headers)){
          $replyHeader = "Thank You!";
          $replyMessage = "Thank you for using our quick contact form. We will get back to you as soon as possible.";
          header( "Location: http://localhost/TimeShare/formReturn.php?header={$replyHeader}&message={$replyMessage}" ) ;
      }else{
          $replyHeader = "Oops!";
          $replyMessage = "An Error Occured whilst trying to send the form. Please try again later";
          header( "Location: http://localhost/TimeShare/formReturn.php?header={$replyHeader}&message={$replyMessage}" ) ;
      }
      ?>
      
      //File: formReturn.php
      <body>
      
          <div>
              <h1> <?php echo $_GET['header'] ?> </h1>
              <p> <?php echo $_GET['message'] ?> </p>
          </div>
      </body>
      

      【讨论】:

        【解决方案3】:

        只需更新您的电子邮件以发送电子邮件并仅在以下情况下重定向

        if (isset($_POST["email"])){
        
        }
        

        否则您知道该表单未提交并且您不想为发送电子邮件而烦恼。

        正如其他人所建议的,您必须在 url 中附加一些 get 参数,例如 Snicksie (header('Location: @987654321@[1|0]');) 建议。然后在您的脚本中检查该变量以查看是否应该显示任何内容。

        例如

        if (isset($_GET["success"])){
           if ($_GET["success"] == 1){
               show success message
           } else {
               show error message
           }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-14
          • 1970-01-01
          • 1970-01-01
          • 2013-09-04
          • 2015-07-25
          相关资源
          最近更新 更多