如何在初始成功后阻止表单多次发送电子邮件. [英] How to stop form from sending email more times after initial success.

查看:26
本文介绍了如何在初始成功后阻止表单多次发送电子邮件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我展示示例代码或指定我做错了什么,因为下面的解决方案并没有给我带来多少运气:(
我正在处理使用 jquery 和 php 分别验证和发送电子邮件的联系表单.在用户成功提交表单之前,表单工作正常.提交表单后,成功消息会成功显示在同一页面上,但是,在刷新页面时会发送另一封电子邮件,因此如果我进行无数次刷新,我的邮箱中会收到无数邮件.

Can someone please show me example code or specify what i am doing wrong as solutions below have not yielded much luck for me :(
I am working on a contact form which uses jquery and php to validate and send email respectively. The form works fine until the point after the user successfully submits the form. Once the form has been submitted, a success message is successfully displayed on the same page however, upon referesh the page sends another email, thus if I do countless refreshes, I would get countless mails in my box.

//handler.php
if(isset($_POST['submit'])) {
    if(!isset($hasError)) {
        $body = "Name: $name \n\nEmail: $email \n\nEnquiry: $enquiry";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
        header("location: ../about/?action=sent");
        exit();
}   

//index.php
var_dump($_GET);
<?php if(isset($hasError)) { ?> 
    <p class="error">Please make sure you have filled all fields with valid information. Thank you.</p>
<?php } ?>                        
<?php if(isset($emailSent) && $emailSent == true) { ?>
    <p><strong>Your enquiry was sent successfully.</strong></p>
    <p>Thank you for your enquiry! Your email was successfully sent and we will be in touch with you promptly.</p>
<?php }; ?>                       
<form id="contactform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
        <legend>Enquiry form</legend>
        <label for="name">Name:</label><input type="text" size="50" name="name" id="name" value="" class="required" />
        <label for="email">Email:</label><input type="text" size="50" name="email" id="email" value="" class="required email" />
        <label for="enquiry">Enquiry:</label><textarea rows="5" cols="20" name="enquiry" id="enquiry" class="required"></textarea>
        <input type="submit" name="submit" value="Submit enquiry" class="curved-btn"></input>
        <input type="hidden" id="form-id" name="form-id" value="general"></input>
    </fieldset>
</form>

任何帮助将不胜感激:)

Any help would be greatly appreciated :)

推荐答案

您应该使用 在这里发布/重定向/获取模式.

使用此模式提倡发送重定向作为表单处理页面的响应,因此后续刷新不会多次触发这些操作.

Using this pattern advocates sending a redirect as a response from your form handling page, and as a result subsequent refreshes will not trigger these actions more than once.

来自文档:

...不是直接返回网页,而是POST操作返回重定向命令.

...instead of returning a web page directly, the POST operation returns a redirection command.

在伪代码中,您的处理程序页面应具有以下内容:

In pseudo-code, your handler page should have something like:

  • 无论您拥有什么初始处理代码
  • 发送电子邮件
  • header("位置:/someotherpage");
  • 退出();

这篇关于如何在初始成功后阻止表单多次发送电子邮件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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