表单重定向问题 [英] Form redirection problem
问题描述
可能的重复:
如何阻止表单发送更多电子邮件最初成功后的时间.
首先,我使用目录构建了我的网站.所以基本上每个页面都是一个目录,在那个目录中,我有一个名为 index.php 的文件.
First of all, i have structured my website using directories. So basically every page is a directory and in that directory, i have a file called index.php.
我的网站上有四个联系表格,目前,似乎都可以使用 hnadler.php 文件.处理程序文件验证数据,检查发布的表单 ID,并基于此适当地路由电子邮件.如果成功发送,将显示成功消息.但是,我当前的实现存在缺陷,如果用户刷新,则会发送另一封邮件.我怎样才能用我现有的代码解决这个问题?谢谢
I have four contact forms on my site, and at the moment, the seem to all work using the hnadler.php file. The handler file validates the data, checks the form-id posted and based on that, it routes the email appropraitely. A success message is displayed if successfully sent. However, my current implimentation is flawed in that if the user refreshes, another mail is sent. How can i solve this with my existing code? Thank you
//handler.php
<?php
if(isset($_POST['submit'])) {
//carry out validation
if(!isset($hasError)) {
//check the form id posted and set email address in $emailTo accordingly
$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;
}
}
//index.php
<?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>
?>
推荐答案
浏览器将/可以在刷新时重新发布数据,因此它看起来像一个新请求.
The browser will/can repost the data on refresh, so it will look like a new request.
快速解决方法是在表单提交后重定向:
A quick fix is to redirect after the form submission:
header("Location: success.php");
这样,如果刷新他们刷新成功页面,而不是您发布到的页面.
That way if the refresh they refresh the success page, not the page you posted to.
这篇关于表单重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!