dongwo5686 2016-10-04 13:30
浏览 628
已采纳

如何在ajax提交后清除表单?

I have a small contact form. Sending messages is working but the form stays with text after submitting. I searched and tried some code to clear it with no success.

// JavaScript Document
$('#contact-form').submit(function (e) {
"use strict";
e.preventDefault();
$.ajax({
    type: "POST",
    url: "contactform.php",
    data: $(this).serialize(), //get form values
    sucess: function (data) {
        document.getElementById("contact-form").reset();
    }

}).done(function (data) {
    console.log(data); // will contain success or invalid 
});
});

And some php

<?php
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$message = $_POST["message"];

$EmailTo = "marcin@rmonline.com";
$Subject = "New Message Received";

// prepare email body text
$email_content .= "Firstname: $firstname
";
$email_content .= "Lastname: $lastname
";
$email_content .= "Email: $email
";
$email_content .= "Phone: $phone
";
$email_content .= "Message: $message";

// send email
$success = mail($EmailTo, $Subject, $email_content, "From:".$email);

// redirect to success page
if ($success){

    echo "success";

} else {
    echo "invalid";
} 
?>

And html

<form id="contact-form" method="post" action="contactform.php" role="form">
  <div class="messages"></div>
  <div class="form-group">
    <input id="form_name" type="text" name="firstname" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <input id="form_lastname" type="text" name="lastname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
    <div class="help-block with-errors"></div>
  </div>
  <div class="form-group">
    <textarea id="form_message" name="message" class="form-control" placeholder="Message for us *" rows="4" required data-error="Please,leave us a message."></textarea>
    <div class="help-block with-errors"></div>
  </div>
  <button type="submit" class="btn" value="Send message">Send Message</button>
  <p class="text-muted"><strong>*</strong> These fields are required.</p>
</form>

I've got the feeling that I'm missing something, but don't know what yet.

  • 写回答

2条回答 默认 最新

  • dongzheng4556 2016-10-04 14:22
    关注

    You can achieve it by using jQuery, put it after ajax success response.

    $('#contact-form')[0].reset(); 
    

    OR

    $(this).closest('form').find("input[type=text], textarea").val("");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能