Php 只有1个数据提交到数据库(食品订购系统)
我是在PHP上实现Ajax的新手,所以我需要很大的帮助!无论如何,我的问题是我的表格菜单上有5个列名 表u菜单: 菜单id 菜单名称 菜单价格 菜单数量 菜单图像 在这些列名中有6个数据 我将在我的网页(users.php)上获取这些数据Php 只有1个数据提交到数据库(食品订购系统),php,ajax,Php,Ajax,我是在PHP上实现Ajax的新手,所以我需要很大的帮助!无论如何,我的问题是我的表格菜单上有5个列名 表u菜单: 菜单id 菜单名称 菜单价格 菜单数量 菜单图像 在这些列名中有6个数据 我将在我的网页(users.php)上获取这些数据 要澄清要提交哪些字段,可以执行以下操作: $(".btnaddorder").click(function() { // here you find closest form tag to the pressed button var form
要澄清要提交哪些字段,可以执行以下操作:
$(".btnaddorder").click(function() {
// here you find closest form tag to the pressed button
var form = $(this).closest('form');
// replace all you values with simple .serialize() function
// this function will use all non-disable fields from the form
$.post("addorders.php", form.serialize(), function(data) { });
});
在服务器端,您的$\u POST
将与表单上的字段具有相同的键。因为js如何理解您需要.menuimage
项中的哪一项?@u\u mulder是的,这就是为什么我如何告诉js将所需数据动态存储到数据库中的原因您的假设是错误的,在menufoodname_1 menufoodname_2等中增加每个输入的数量,还应怀疑只有第一个产品提交的原因是您有多个表单具有相同的输入名称谢谢您的回复,但我最近尝试了序列化功能,但它不起作用。请帮我试用我的代码好吗?如果是-您在开发人员控制台中看到了什么错误或任何意外行为?是的,先生,它说“未捕获的TypeError:$(…)。最近的不是函数”您的jquery版本是什么?3-3-1缩微js和3-3-7缩微js先生。
<script type="text/javascript">
$(document).ready(function() {
$(".btnaddorder").click(function() {
var menuimage = $(".menuimage").val();
var menufoodname = $(".menufoodname").val();
var menuprice = $(".menuprice").val();
var menuquantity = $(".menuquantity").val();
// Returns successful data submission message when the entered information is stored in database.
$.post("addorders.php", {
menuimage1: menuimage,
menufoodname1: menufoodname,
menuprice1: menuprice,
menuquantity1: menuquantity,
}, function(data) {
});
});
});
</script>
<?php
include 'config/initialize.php';
include 'credentials/credentialsForUsers.php';
$menuimage = $_POST['menuimage1'];
$menufoodname = $_POST['menufoodname1'];
$menuprice = $_POST['menuprice1'];
$menuquantity = $_POST['menuquantity1'];
$sql = "INSERT INTO table_orders VALUES('','$username','$email','$menuimage','$menufoodname','$menuprice','$menuquantity')";
$result = mysqli_query($con, $sql);
?>
$(".btnaddorder").click(function() {
// here you find closest form tag to the pressed button
var form = $(this).closest('form');
// replace all you values with simple .serialize() function
// this function will use all non-disable fields from the form
$.post("addorders.php", form.serialize(), function(data) { });
});