php - 如何使用 PHP 将 HTML 表单提交到 MySQL,并在提交时刷新(AJAX?)

标签 php html mysql ajax

我有一个 2 输入表单,我想在 MySQL 查询的 where 语句中使用它来返回网页上的数据

<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',
            success: function () 
          });

        });

      });
    </script>
</head>

我正在寻找一个包含来自 MySQL 服务器的数据的表,但页面重新加载时字段被清空

更新以添加新代码

<html>
   <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
  $(function () {

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

          e.preventDefault();

          $.post("3.16.86.231/bandi.php",  {
                // these are the data you are passing you will to find a way to get them from the input boxes 
              po: '$porder',
              pn: '$pnumber'
  },
        });

      });
    </script>
  </head><style>
 body {
 font-family:arial;
 font-size:12;
 }
</style>
<form action = "" method = "POST">
<body>
<table border = 3>
<tr>
<td> Purchase Order Number </td>
<td> <input type = text name = po value = "<?php echo $porder;?>" size="50" autofocus>
</tr>
<tr>
<td> Part Number </td>
<td> <input type = text name = pn value = "<?php echo $pnumber;?>" size="50">
</tr>
<tr>
<td colspan = 3>
<input type = "submit" name="send" value = "Ok" title="Click here to display values.">
<input type = "submit" name="clear" value = "Clear" title="Click here to clear text boxes.">
</td>
</tr>
</table>
</form>

最佳答案

您可以通过不同的方式执行此操作,但在这两种情况下,您都需要准备好 php 文件并将其存储在服务器中可以运行的位置。然后,您需要找到 php 文件的 url,因此,如果您的 php 文件名为 Script.php,那么您的 url 是 localhost/Script.php 您可以通过在浏览器中输入 url 来确认,看看是否收到错误 404或不。

找到您的网址后,我们就可以开始提交您的请求。所以这是计划,您将把一些信息从 html/js 发送到您的 php 代码。然后,您的 php 代码将连接到数据库并运行您的 sql。因此,要将信息发送到 php,您可以使用表单 post 方法,例如:

<form action="localhost/script.php" method="get">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>
或者您可以像上面的示例中那样使用 AJAX。但是,如果您要使用 AJAX,那么您需要将代码修改为如下所示:

  $(function () {

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

          e.preventDefault();

          $.post("localhost/script.php",  {
                // these are the data you are passing you will to find a way to get them from the input boxes 
              name: "Donald Duck",
              city: "Duckburg"
  },
  function(data, status){
  //this is the data you are getting back and you need to find a way to put them in a table
    alert("Data: " + data + "\nStatus: " + status);
  });
        });

      });

如果您要使用表单发布方法,那么您将需要让您的 php 脚本呈现您的表并将数据放入其中。

还有其他方法可以做到这一点,但这应该可以帮助您入门

更多信息来源: https://www.w3schools.com/tags/tag_form.asp

https://www.w3schools.com/jquery/jquery_ajax_get_post.asp

关于php - 如何使用 PHP 将 HTML 表单提交到 MySQL,并在提交时刷新(AJAX?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55639513/

相关文章:

php - 在 bootstrap 4 中使用 font-awesome 图标时如何调整表格行高?

mysql - 如何获取MySQL中特定值之前和之后的行

PHP 回显 HTML block 但收到意外结果

javascript - jquery .html 仅在 Chrome 中重新加载页面

php - 在php在线考试系统中使用rand order时问题重复

html - 将元素放置在滚动 div 中的固定位置

mysql - 我如何加入这些查询?

php - Flex 应用程序通过 PHP 从 MySql 获取数据 - 示例

php - 从远程服务器或 URL 复制文件

php - 根据星期几获取价格