php联系表单,回复邮件

时间:2011-09-23 11:31:03

标签: php

我创建了一个联系表单,使用我的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文件,我最终得到了一个重定向循环。这很糟糕!

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

3 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

您可以将其作为获取变量传递

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

答案 2 :(得分:0)

只需更新您的电子邮件,即可发送电子邮件并重定向

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

}

否则您知道该表单未提交且您不想打扰发送电子邮件。

和其他人建议的那样,你必须在网址上添加一些get参数,比如Snicksie(header('Location: http://localhost/TimeShare/formReturn.php?success=[1|0]');) 建议。然后在你的脚本上检查该变量,看看你是否应该显示任何内容。

e.g。

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