doy57007 2014-08-21 03:59
浏览 47
已采纳

php表单 - 提交时的清晰表单 - 重置它

I have the following form that works. But if a user was to click refresh, it resends the form over and over on each refresh. How can I clear the form on refresh so that it is required to fill out again?

Thanks

PHP

This is a simple contact form

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



<?php
//fields
    $honeypot   = '';
    $error      = '';
    $name       = '';
    $email      = '';
    $comments   = '';

    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) == '') {
        $error = '<div class="error_message">Need your name</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) == '') {
        $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
Email:   $email 

Message:
$comments


";

    $msg = $e_body . $e_content . $e_reply;
    if(mail($address, $e_subject, $msg, "From: $email
Reply-To: $email
Return-Path: $email
"))
    {
    //success html page response
     echo "<div id='succsess_page'>";
     echo "<h1>Email Sent Successfully.</h1>";
     echo "<p>Thank you  <strong>$name</strong> ,  your message has been submitted.        </p>";
         echo "</div>";

         } else echo "Error. Mail not sent";
        }
    }
        if(!isset($_POST['contactus']) || $error != '') // Do not edit.

    {

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

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

<p class="form1">Email: <input name="email" type="text" id="email" size="30" value="<?php echo $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"><?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>
  • 写回答

2条回答 默认 最新

  • dousi6405 2014-08-21 04:05
    关注

    You can redirect the page after inserting the data using :

    header('Location:'.$_SERVER['PHP_SELF']);
    

    After redirecting, the whole page will be reloaded and refreshing won't cause data to be inserted again.

    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) == '') {
            $error = '<div class="error_message">Need your name</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) == '') {
            $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
    Email:   $email 
    
    Message:
    $comments
    
    
    ";
    
        $msg = $e_body . $e_content . $e_reply;
        if(mail($address, $e_subject, $msg, "From: $email
    Reply-To: $email
    Return-Path: $email
    "))
        {
        //success html page response
         echo "<div id='succsess_page'>";
         echo "<h1>Email Sent Successfully.</h1>";
         echo "<p>Thank you  <strong>$name</strong> ,  your message has been submitted.        </p>";
             echo "</div>";
    
             } else echo "Error. Mail not sent";
           }
            header('Location:'.$_SERVER['PHP_SELF']);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?