Ajax jQuery不发布表单数据

时间:2019-04-07 15:39:05

标签: javascript php jquery html ajax

当我单击表单上的提交按钮时,我的注册页面将刷新,而表单数据不会发布到数据库中。

我的HTML标记看起来像这样-

  <form id="reg">
<div class="form-group">
  <label style="color: white; text-align:center" for="username">Username</label>
  <input style="color: #C0C0C0;" type="text" name = "username" class="form-control" id="username" placeholder="Enter Username">
</div>
<div class="form-group">
  <label style="color: white; text-align:center" for="password">Password</label>
  <input style="color: #C0C0C0;" type="password" name = "password" class="form-control" id="password" placeholder="Password">
</div>
<div class="form-group">
  <label style="color: white; text-align:center" for="email">Email</label>
  <input style="color: #C0C0C0;" type="text" name = "email" class="form-control" id="email" placeholder="Enter Email">
</div>
<button type="submit" name = "submit" class="btn btn-primary">Submit</button>

我的jquery代码-

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <script>

  $('#reg').on('submit', function(e) {
    e.preventDefault 
    $.ajax({
      url:'script/register.php',
      data: $(this).serialize(), 
      type:'POST',
      success:function(results) {
        $(".result").html(results);
      }
    });
  });

  </script>

我不确定我要去哪里。页面刷新,我在url栏中看到表单变量和值。以上任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

检查语法:preventDefault是一个函数。

 $('#reg').on('submit', function(e) {
    e.preventDefault() <---- Here fails 
    $.ajax({
      url:'script/register.php',
      data: $(this).serialize(), 
      type:'POST',
      success:function(results) {
        $(".result").html(results);
      }  
   });
});

答案 1 :(得分:0)

用以下

替换您的ajax代码
((?=.*[a-z]{2,})(?=.*[A-Z]{2,})(?=.*[0-9]{2,})|(?=.*[a-z]{2,})(?=.*[A-Z]{2,})(?=.*[!@#$%&\/=?_.,:;\\-]{2,})|(?=.*[a-z]{2,})(?=.*[0-9]{2,})(?=.*[!@#$%&\/=?_.,:;\\-]{2,})|(?=.*[A-Z]{2,})(?=.*[0-9]{2,})(?=.*[!@#$%&\/=?_.,:;\\-]{2,})).{8,}

您应该返回false以防止页面刷新。