简体   繁体   English

提交后清除表单字段

[英]Clear form field after submit

I have read other answers for the same question but I am having problems and would be grateful for some advice. 我已经阅读了同一问题的其他答案,但是我遇到了问题,将不胜感激。 I have javaScript in my html file, and an Onclick() statement on the submit button to clear the form but now the email confirmation message does not come up and the message is no longer sent. 我的html文件中包含javaScript,并且在“提交”按钮上有一个Onclick()语句以清除表单,但是现在电子邮件确认消息没有出现并且不再发送该消息。 If I put the onClick(); 如果我把onClick(); in the body of the form, every field is cleared just by clicking on a form field. 在表单主体中,只需单击表单字段即可清除每个字段。 I really want to be able to submit a message, then have the form cleared on successful send. 我真的很希望能够提交一条消息,然后在成功发送时清除表格。

   <script type ="text/javascript"> 
            function clearform () {
 document.getElementById("name").value="";
 document.getElementById("email").value="";
 document.getElementById("subject").value="";
 document.getElementById("message").value="";
}
</script>

            <div class="row">
                <div class="col-sm-6">
                    <h2>Send us a message</h2>
                    <!-- action="replace it with active link."-->
                    <form action="contact.php" method="post" name="contact-form" id="contact-form" >
                        <label for="name">Your Name <span>*</span></label>
                        <input type="text" name="name" id="name" value="" required />
                        <label for="email">Your E-Mail <span>*</span></label>
                        <input type="text" name="email" id="email" value="" required />
                        <label for="subject">Subject</label>
                        <input type="text" name="subject" id="subject" value="" />
                        <label for="message">Your Message</label>
                        <textarea name="message" id="message"></textarea>
                        <div class="row">
                            <div class="col-sm-6">
                                <input type="submit" name="sendmessage" id="sendmessage" value="Submit" onclick="clearform();" />
                            </div>
                            <div class="col-sm-6 dynamic"></div>
                        </div>
                        </form>

I then have the following in the PHP file: 然后,我在PHP文件中包含以下内容:

    private function sendEmail(){
    $mail = mail($this->email_admin, $this->subject, $this->message,
         "From: ".$this->name." <".$this->email.">\r\n"
        ."Reply-To: ".$this->email."\r\n"
    ."X-Mailer: PHP/" . phpversion());

    if($mail)
    {
        $this->response_status = 1;
        //$this->response_html = '<p>Thank You!</p>';
    }
}


function sendRequest(){
    $this->validateFields();
    if($this->response_status)
    {
        $this->sendEmail();
    }

    $response = array();
    $response['status'] = $this->response_status;   
    $response['html'] = $this->response_html;

    echo "<span class=\"alert alert-success\" >Your message has been received. Thanks!</span>";
    header("Location: contact.php");// redirect back to your contact form
    exit;


   }

}


$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();

?>

You've got to make sure the event continues to propagate and the form is submitted: 您必须确保事件继续传播并提交表单:

function clearform () {
    document.getElementById("name").value="";
    document.getElementById("email").value="";
    document.getElementById("subject").value="";
    document.getElementById("message").value="";
    return true;
}

No ajax form post 没有ajax表格发布

If you are not using ajax to submit the form (you don't seem to be using it), there is no need for javascript to clear the form, the form will be reloaded on submit and it will be empty. 如果您不使用ajax提交表单(您似乎没有使用它),则无需使用javascript清除表单,表单将在提交时重新加载,并且为空。

However, you have a problem with your location redirect: You are outputting html before that so the redirect will probably fail. 但是,您的location重定向存在问题:在此之前您正在输出html,因此重定向可能会失败。

You should not output anything before you redirect and you could add a query variable to the url so that you can show your success message when the form loads: 在重定向之前,您不应该输出任何内容,并且可以将查询变量添加到url,以便在加载表单时显示成功消息:

if($this->response_status)
{
    $this->sendEmail();
}

header("Location: contact.php");// redirect back to your contact form
exit;

Using ajax to post the form 使用ajax发布表单

If you are using ajax (the setting of your response variables seems to indicate that you want to do that), you should put the clearform () call in the success function of your ajax call and remove the header() redirect in php. 如果您使用的是ajax(响应变量的设置似乎表明您想要这样做),则应将clearform ()调用放入ajax调用的成功函数中,并删除php中的header()重定向。 Instead you probably want to return / output the results: 相反,您可能想返回/输出结果:

if($this->response_status)
{
    $this->sendEmail();
}

$response = array();
$response['status'] = $this->response_status;   
$response['html'] = $this->response_html;

echo json_encode($response);
exit;

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

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