Javascript 如何清除输入字段,但将数据推送到数据库?

Javascript 如何清除输入字段,但将数据推送到数据库?,javascript,php,ajax,Javascript,Php,Ajax,我正试图清除输入字段,但当我清除它们时,php会将空字符串推送到我的数据库。有人能帮我吗 这是我的密码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X

我正试图清除输入字段,但当我清除它们时,php会将空字符串推送到我的数据库。有人能帮我吗

这是我的密码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
     <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'phpAdd.php',
            data: $('form').serialize(),
            success: function () {
              alert('form was submitted');
            }
          });

        });

      });

      function submitForm(){
          var form = document.getElementById("myForm");
          form.reset(); 
          $('#firstname').focus();
    }
    </script>
</head>
<body>
    <center>
        <form action="phpAdd.php" method="post" id="myForm">
        <h1>Please enter your info.</h1>
        First Name: <input type="text" name="firstname" id="firstname" class="firstname"><br>
        Last Name:  <input type="text" name="lastname" id="lastname" class="lastname"> <br>
        <input type="submit" value="SUBMIT" class="SUBMIT" onclick="submitForm()">
       </form>
    </center>
</body>
</html>

文件
$(函数(){
$('form')。关于('submit',函数(e){
e、 预防默认值();
$.ajax({
键入:“post”,
url:'phpAdd.php',
数据:$('form')。序列化(),
成功:函数(){
警报(“表格已提交”);
}
});
});
});
函数submitForm(){
var form=document.getElementById(“myForm”);
form.reset();
$(“#firstname”).focus();
}
请输入您的信息。
名字:
姓氏:

将数据推送到php后,如何清除字段的?

将表单重置放在ajax调用的成功块中,重置之前是通过它的外观调用的,而不是通过它的帮助将表单重置放在ajax调用的成功块中,重置之前是通过它的外观调用的,不太高兴它有帮助