php简单联系表单重新提交刷新浏览器

时间:2016-10-13 05:44:12

标签: javascript php jquery html forms

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

代码:

<!--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));
}
?>

2 个答案:

答案 0 :(得分:1)

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

在使用表单数据后,只需将其重定向到同一页面。 实施例 -

header('location:yourpage.php');

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

unset($_POST);

答案 1 :(得分:0)

您可以使用javascript重定向:

import * as moment from 'moment/moment';

console.log(this.today = moment().startOf('day').fromNow());