gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:07:02 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com