gpt4 book ai didi

javascript - php简单联系表单在浏览器刷新时重新提交

转载 作者:太空宇宙 更新时间:2023-11-04 16:28:39 24 4
gpt4 key购买 nike

以下表单按预期工作,包括蜜 jar ,但如果有人在完成表单之前单击浏览器中的“刷新”,它会尝试重新提交。我已经尝试过 session ,但它不起作用。有谁知道如何使页面重定向回 contact.php 或结束 session ,以便在浏览器中刷新时不会重新提交? (我花了两天时间寻找答案)。谢谢

代码:

<!--form-->
<?php
//fields
$link_address = 'contact.php'; // page to redirect to home page
$honeypot = '';
$error = '';
$name = 'Name (required)';
$email = 'Email (required)';
$comments = 'Message (required)';

if(isset($_POST['contactus'])) {

$honeypot = $_POST['honeypot'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

// honeypot
if($honeypot)
exit(1);


//error messages
if(trim($name) == 'Name (required)') {
$error = '<div class="error_message">Please enter your Name</div>';
} else if(trim($name) == '') {
$error = '<div class="error_message">Please enter your Name</div>';

} else if(trim($email) == 'Email (required)') {
$error = '<div class="error_message">Please enter an email address</div>';
} else if(trim($email) == '') {
$error = '<div class="error_message">Please enter an email address</div>';

} else if(!isEmail($email)) {
$error = '<div class="error_message">Please enter a valid email address</div>';

} else if(trim($comments) == 'Message (required)') {
$error = '<div class="error_message">Please enter a Message</div>';
} else if(trim($comments) == '') {
$error = '<div class="error_message">Please enter a Message</div>';

}
if($error == '') {
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}

//email address
$address = "email@youremail.com";
//email message
$e_subject = 'Web Message from: ' . $name . '.';
$e_body = "From: $name\nEmail: $email \r\n\nMessage:\n$comments\n\n\n";

$msg = $e_body . $e_content . $e_reply;
if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"))
{
//success html page response
echo "<div id='success_page'>";
echo "<div id='thanks'>";
echo "<h1>Message Sent Successfully.</h1>";
echo "<p id='cprint1'>Thank you. Your message was sent to us. </br>We will be in touch shortly.</p>";
echo "<p id='print'><a href='javascript:window.print()'>Print your Message</a></p>";
echo "<p id='cclose'><a href='$link_address'>Close this Message</a></p>";
echo "</div>";
echo "<div id='thanks2'>";
echo "<div id='titlemsg'>Below is your message</div>";
echo "<p><span>Name:</span> $name</p>";
echo "<p><span>Email:</span> $email</p>";
echo "<p><span>Message:</span>$comments</p>";
echo "</div>";
echo "</div>";
} else echo "Error. Mail not sent";
}
}
if(!isset($_POST['contactus']) || $error != '') // Do not edit.
{

?>
<!--form-->



<form method="post" action="" id="myform" autocomplete="off">
<div id="error"><?php echo $error; ?></div>

<input name="name" type="text" id="name" class="form1"
value="<?php echo $name; ?>" onfocus="if(this.value == 'Name (required)') {this.value = ''; }" onblur="if(this.value == '') { this.value = 'Name (required)'; }" value="Name (required)" />

<input name="email" type="text" id="email" class="form1"
value="<?php echo $email; ?>" onfocus="if(this.value == 'Email (required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Email (required)'; }" value="Email (required)" />

<input name="honeypot" type="text" id="honeypot" class="form2" value="<?php echo $honeypot; ?>" />

<textarea name="comments" cols="40" rows="3" id="comments"
value="<?php echo $comments; ?>" onfocus="if(this.value == 'Message (required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Message (required)'; }" value="Message (required)"><?php echo $comments; ?></textarea>

<input name="contactus" type="submit" class="submit" id="contactus" value="Contact us" />


</form>
<?php }
function isEmail($email) { // Email address verification, do not edit.
return(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,12})$/",$email));
}
?>

最佳答案

成功发送邮件后,您可以在 session 中保存消息。然后刷新页面并检查 session 是否存在,然后显示消息并重置 session 。

使用表单数据后只需将其重定向到同一页面即可。示例-

header('location:yourpage.php');

您也可以在将 $_POST 插入数据库后取消设置。

unset($_POST);

关于javascript - php简单联系表单在浏览器刷新时重新提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40013157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com