小编典典

使用AJAX进行表单提交,无需刷新页面即可将表单数据传递给PHP

ajax

谁能告诉我为什么这部分代码不起作用?

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {
        $('form').bind('submit', function () {
          $.ajax({
            type: 'post',
            url: 'post.php',
            data: $('form').serialize(),
            success: function () {
              alert('form was submitted');
            }
          });
          return false;
        });
      });
    </script>
  </head>
  <body>
    <form>
      <input name="time" value="00:00:00.00"><br>
      <input name="date" value="0000-00-00"><br>
      <input name="submit" type="button" value="Submit">
    </form>
  </body>
</html>

当我推送提交时,什么也没有发生。在接收的php文件中,我使用$ _POST [‘time’]和$ _POST
[‘date’]将数据放入mysql查询中,但它没有获取数据。有什么建议?我假设这与“提交”按钮有关,但我无法弄清楚


阅读 190

收藏
2020-07-26

共1个答案

小编典典

表单是在ajax请求之后提交的。

<html>
  <head>
    <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: 'post.php',
            data: $('form').serialize(),
            success: function () {
              alert('form was submitted');
            }
          });

        });

      });
    </script>
  </head>
  <body>
    <form>
      <input name="time" value="00:00:00.00"><br>
      <input name="date" value="0000-00-00"><br>
      <input name="submit" type="submit" value="Submit">
    </form>
  </body>
</html>
2020-07-26