简体   繁体   English

php联系表单,回复邮件

[英]php contact form with reply message

I created a contact form that uses my php script to email the form parameters: 我创建了一个联系表单,使用我的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' ) ;
?>

on submit the form action points to this code. 提交表单操作指向此代码。

Now if the mail function is successfull, i want the form to redirect to formReturn.php, however i want the content in a particular div to show the $replyHeader and $replyMessage. 现在,如果邮件功能成功,我希望表单重定向到formReturn.php,但是我希望特定div中的内容显示$ replyHeader和$ replyMessage。

So once the form is posted, it redirects to a page that either displays a successfull message or an error message. 因此,一旦表单发布,它就会重定向到显示成功消息或错误消息的页面。

Now on my formReturn.php page ive tried including the variables Like so: 现在在我的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>

Problem with this code is, because im including the email.php file, i end up with a redirection loop. 这段代码的问题是,因为我包括email.php文件,我最终得到了一个重定向循环。 Which is bad! 这很糟糕!

So how would i get the variables $replyHeader and $replyMessage onto the page in that specific location depending on the success or failure of the mail() function 那么我如何将变量$ replyHeader和$ replyMessage放到该特定位置的页面上,具体取决于mail()函数的成功或失败

You can also add a GET-variable to your second page: 您还可以向第二页添加GET变量:

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

Then you can add the message in your other page. 然后,您可以在其他页面中添加该消息。

you could pass it as a get variable 你可以将它作为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>

just update your email thing to send email and redirect ONLY IF 只需更新您的电子邮件,即可发送电子邮件和重定向

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

}

otherwise you know that form is not submitted and that you don't want to bother about sending email. 否则你知道表单没有提交,你不想打扰发送电子邮件。

and as others suggested, you have to append some get param to the url, like Snicksie ( header('Location: http://localhost/TimeShare/formReturn.php?success= [1|0]'); ) suggested. 并且正如其他人建议的那样,你必须在网址上附加一些get参数,比如Snicksie( header('Location: http://localhost/TimeShare/formReturn.php?success= [1|0]'); )建议。 then on your script you check for that variable to see if you should show anything. 然后在你的脚本上检查该变量,看看你是否应该显示任何内容。

eg 例如

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

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

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