PHP表单 - 提交时清除表单 - 重置它 [英] php form - clear form on submission - reset it

查看:180
本文介绍了PHP表单 - 提交时清除表单 - 重置它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的表单可以使用。但是如果用户点击刷新,它会在每次刷新时重复发送表单。如何在刷新时清除表单以便再次填写?



谢谢

PHP

这是一个简单的联系表单



 < 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
//字段
$ honeypot ='';
$ error ='';
$ name ='';
$ email ='';
$ comments ='';

if(isset($ _ POST ['contactus'])){

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

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


//错误信息
if(trim($ name)==''){
$ error ='< div class =error_message> ;需要你的名字< / div>';
} else if(trim($ email)==''){
$ error ='< div class =error_message>需要您的电子邮件< / div>';
} else if(!isEmail($ email)){
$ error ='< div class =error_message>需要有效的电子邮件< / div>';
} else if(trim($ comments)==''){
$ error ='< div class =error_message>需要消息< / div>;

if($ error ==''){
if(get_magic_quotes_gpc()){
$ comments = stripslashes($ comments);
}
//电子邮件地址
$地址=email@example.com;
//电子邮件消息
$ e_subject ='网络消息来自:'。 $名称。 。;
$ e_body =From:$ name\\\
Email:$ email \r\\\
\\\
Message:\\\
$ comments\\\
\\\
\\\
;

$ msg = $ e_body。 $ e_content。 $ e_reply;
if(mail($ address,$ e_subject,$ msg,From:$ email\r\\\
Reply -To:$ email\r\\\
Return-Path:$ email\r\\\
))
{
//成功html页面响应
echo< div id ='succsess_page'>;
回显< h1>电子邮件已成功发送。< / h1>;
echo< p>感谢您< strong> $ name< / strong>,您的讯息已提交。< / p>;
回显< / div>;

}其他回显错误。邮件未发送;
}

if(!isset($ _ POST ['contactus'])|| $ error!='')//不要编辑。

{

?>
<?php echo $ error; ?>
<! - form - >
< form method =postaction =>

< p class =form1>名称:< input name =nametype =textid =namesize =30value =<?php echo $ name;?>的onblur = TOUPPER(THIS.VALUE); />< / p为H.

< p class =form1>电子邮件:< input name =emailtype =textid =emailsize =30value =<?php echo $ email;?> />< / p为H.
< p class =form2>确认电子邮件地址:< input name =email_confirmtype =textid =email_confirmsize =30value =<?php echo $ email_confirm ;?> />< / p为H.

< p class =form1>消息:< textarea name =commentscols =40rows =3id =comments> $意见; ?>< / textarea的>< / p为H.

< p class =form1>< input name =contactustype =submitclass =submitid =contactusvalue =Submit/><< ; / p为H.

< / form>
<! - 结束表单 - >

<?php}

函数isEmail($ email){//电子邮件地址验证,请勿编辑。
return(preg_match(/ ^ [_ a-z0-9 - ] +(\。[_ a-z0-9 - ] +)* @ [a-z0-9 - ] +(\。[一个-Z0-9 - ] +)*(\ [AZ] {2,12})$ /,$电子邮件));
}

?>

<! - > - 首字母大写输入名称---->
< script>
函数toUpper(mystring){
var sp = mystring.split('');
var wl = 0;
var f,r;
var word = new Array();
for(i = 0; 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;
返回true;
}
< / script>


解决方案

插入数据后,您可以使用以下方法重定向页面:

  header('Location:'。$ _ SERVER ['PHP_SELF']); 

重定向后,整个页面将被重新加载,刷新不会导致数据再次被插入。

  if(isset($ _ POST ['contactus'])){

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

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


//错误信息
if(trim($ name)==''){
$ error ='< div class =error_message> ;需要你的名字< / div>';
} else if(trim($ email)==''){
$ error ='< div class =error_message>需要您的电子邮件< / div>';
} else if(!isEmail($ email)){
$ error ='< div class =error_message>需要一个有效的电子邮件< / div>';
} else if(trim($ comments)==''){
$ error ='< div class =error_message>需要消息< / div>;

if($ error ==''){
if(get_magic_quotes_gpc()){
$ comments = stripslashes($ comments);
}
//电子邮件地址
$地址=email@example.com;
//电子邮件消息
$ e_subject ='网络消息来自:'。 $名称。 。;
$ e_body =From:$ name\\\
Email:$ email \r\\\
\\\
Message:\\\
$ comments\\\
\\\
\\\
;

$ msg = $ e_body。 $ e_content。 $ e_reply;
if(mail($ address,$ e_subject,$ msg,From:$ email\r\\\
Reply -To:$ email\r\\\
Return-Path:$ email\r\\\
))
{
//成功html页面响应
echo< div id ='succsess_page'>;
回显< h1>电子邮件已成功发送。< / h1>;
echo< p>感谢您< strong> $ name< / strong>,您的邮件已被提交。< / p>;
回显< / div>;

}其他回显错误。邮件未发送;
}
header('Location:'。$ _ SERVER ['PHP_SELF']);
}


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\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  <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>

解决方案

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\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  <strong>$name</strong> ,  your message has been submitted.        </p>";
         echo "</div>";

         } else echo "Error. Mail not sent";
       }
        header('Location:'.$_SERVER['PHP_SELF']);
        }

这篇关于PHP表单 - 提交时清除表单 - 重置它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆