繁体   English   中英

php简单的联系表单-停止提交页面刷新

[英]php simple contact form - stop submit on page refresh

下面的PHP形式可以正常工作,除了刷新之外。 如果用户在提交表单后单击刷新按钮,它将重新提交表单。

有没有一种方法可以使用PHP或Jquery刷新页面后停止重新提交表单?

我知道在PHP中应该有一种使用Session的方法,但是我不确定如何使用。

contact.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<style>
.error_message{color:#cc0000;}
.form1{}
.form2{display:none;}
#succsess_page h1{background:url('http://example.com/ok.png')left no-repeat;padding-left:40px;color:#45a015; }
</style>
<h1>This is a simple contact form</h1>

<?php
//fields
        $link_address   = 'contact.php'; // page to redirect to
        $honeypot   = '';
        $error      = '';
        $name       = 'Name';
        $email      = 'Email';
        $comments   = 'Message';

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

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

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


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

        } else if(trim($email) == 'Email') {
            $error = '<div class="error_message">Need your Email</div>';
        } else if(trim($email) == '') {
            $error = '<div class="error_message">Need your Email</div>';

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

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

        }
        if($error == '') {
            if(get_magic_quotes_gpc()) {
                $comments = stripslashes($comments);
        }
//email address
        $address = "email@example.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='succsess_page'>";
         echo "<h1>Email Sent Successfully.</h1>";
         echo "<p>Thank you. The following was sent to us. <br/><br/>$name<br/><br/>$email<br/><br/>$comments</p>";
         echo "<a href='$link_address'>CLOSE THIS MESSAGE</a>";
         echo "</div>";
         } else echo "Error. Mail not sent";
        }
    }
        if(!isset($_POST['contactus']) || $error != '') // Do not edit.
    {

?>
        <?php echo $error; ?>
<!--form-->
<form method="post" action="" id="myform">

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

<p class="form1">Email: <input name="email" type="text" id="email" size="30" class="input2" value="<?php echo $email; ?>" onfocus="if(this.value == 'Email') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Email'; }" value="Email" /></p>
<p class="form2">Confirm Email: <input name="email_confirm" type="text" id="email_confirm" size="30" value="<?php echo $email_confirm; ?>" /></p>

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

<p class="form1"><input name="contactus" type="submit" class="submit" id="contactus" value="Submit" /></p>

</form>
<!--end 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));
}

?>

<!------- capitalize first letter Name input ---->
<script>
function toUpper(mystring) {
var sp = mystring.split(' ');
var wl=0;
var f ,r;
var word = new Array();
for (i = 0 ; i < sp.length ; i ++ ) {
f = sp[i].substring(0,1).toUpperCase();
r = sp[i].substring(1);
word[i] = f+r;
}
newstring = word.join(' ');
document.getElementById('name').value = newstring;
return true;
}
</script>

正确提交表单后,运行以下代码:

header("Location :contact.php?msg=success");

这会将用户重定向到具有允许显示消息的条件的联系表单。

if($_REQUEST['msg']){
 echo "Contact Form Submitted Successfully!!";
}

如果他们点击刷新,它将在再次显示联系表单时显示一条成功消息。

更新:

if(mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"))
    {
       header("Location: contact.php?msg=success");
    }else{
       header("Location: contact.php?msg=error");
    }

然后显示:

<?php if($_REQUEST['msg']=="success"){
    echo "Success Message Here.";
}elseif($_REQUEST['msg']=="error"){
    echo "Error Message Here."
}
<!--form-->
<form method="post" action="" id="myform">

暂无
暂无

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

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