Php 添加记录json传递两个空数据
我创建了一个简单的库存表单。表单由(productid、productname、price、qty、total)组成填写表单并单击“添加”按钮后,所有记录都将成功添加表。当我将数据发送到add.php时。json在顶部传递两个空数据,我不知道为什么会发生这种情况。我在下面附上了一个错误,前两行没有显示数据,后面只显示我添加的数据。请任何人帮我解决这个问题Php 添加记录json传递两个空数据,php,jquery,json,Php,Jquery,Json,我创建了一个简单的库存表单。表单由(productid、productname、price、qty、total)组成填写表单并单击“添加”按钮后,所有记录都将成功添加表。当我将数据发送到add.php时。json在顶部传递两个空数据,我不知道为什么会发生这种情况。我在下面附上了一个错误,前两行没有显示数据,后面只显示我添加的数据。请任何人帮我解决这个问题 0 : {barcode: "", pname: "", pro_price: "", qty: "", total_cost: ""} 1
0
:
{barcode: "", pname: "", pro_price: "", qty: "", total_cost: ""}
1
:
{barcode: "↵ ↵ ", pname: "↵ ↵ ↵ ", pro_price: "↵ ↵ ", qty: "↵ ↵ ", total_cost: "↵ ↵ "}
2
:
{barcode: "111", pname: "bun", pro_price: "20", qty: "2", total_cost: "40"}
3
:
{barcode: "111", pname: "jam", pro_price: "30", qty: "1", total_cost: "30"}
表格
<table class="table table-bordered">
<caption> Add Products </caption>
<tr>
<th>Product Code</th>
<th>Product Name</th>
<th>Price</th>
<th>Qty</th>
<th>Amount</th>
<th>Option</th>
</tr>
<tr>
<td>
<input type="text" class="form-control" placeholder="barcode" id="barcode" name="barcode" required>
</td>
<td>
<label id="pro_name" name="pname" id="pname"></label>
<input type="text" class="form-control" placeholder="barcode" id="pname" name="pname" disabled >
</td>
<td>
<input type="text" class="form-control pro_price" id="pro_price" name="pro_price"
placeholder="price" >
</td>
<td>
<input type="number" class="form-control pro_price" id="qty" name="qty"
placeholder="qty" min="1" value="1" required>
</td>
<td>
<input type="text" class="form-control" placeholder="total_cost" id="total_cost" name="total_cost">
</td>
<td>
<button class="btn btn-success" type="button" onclick="addproduct()">Add
</button>
</td>
</tr>
</table>
<table class="table table-bordered" id="product_list">
<caption> Products</caption>
<thead>
<tr>
<th style="width: 40px">Remove</th>
<th>Product Code</th>
<th>Product Name</th>
<th>Unit price</th>
<th>Qty</th>
<th>Amount</th>
</tr>
</thead>
<tbody></tbody>
</table>
添加产品
产品代码
品名
价格
数量
数量
选项
添加
产品
去除
产品代码
品名
单价
数量
数量
jquery
function addproduct()
{
var barcode = $("#barcode").val();
var pname = $("#pname").val();
var pro_price = $("#pro_price").val();
var qty = $("#qty").val();
var total_cost = $("#total_cost").val();
var markup = "<tr> <td>" + barcode + "</td><td>" + pname + "</td> <td>" + pro_price + "</td> <td>" + qty + "</td> <td>" + total_cost + "</td> </tr>";
$("#product_list tbody").append(markup);
}
function save() {
var table_data = [];
$('table tbody tr').each(function(row,tr)
{
var sub = {
'barcode' : $(tr).find('td:eq(0)').text(),
'pname' : $(tr).find('td:eq(1)').text(),
'pro_price' : $(tr).find('td:eq(2)').text(),
'qty' : $(tr).find('td:eq(3)').text(),
'total_cost' : $(tr).find('td:eq(4)').text(),
};
table_data.push(sub);
});
console.log(table_data);
}
函数addproduct()
{
var barcode=$(“#barcode”).val();
var pname=$(“#pname”).val();
var pro#u price=$(“#pro#u price”).val();
变量数量=$(“#数量”).val();
var总成本=$(“#总成本”).val();
var markup=“”+条形码+“”+pname+“”+专业价格+“”+数量+“”+总成本+“”;
$(“#产品列表体”)。附加(标记);
}
函数save(){
var表_数据=[];
$('table tbody tr')。每个(函数(行,tr)
{
var sub={
'条形码':$(tr).find('td:eq(0)').text(),
'pname':$(tr).find('td:eq(1)').text(),
pro_price:$(tr).find('td:eq(2)').text(),
'数量':$(tr).find('td:eq(3)').text(),
‘总成本’:$(tr).find('td:eq(4)').text(),
};
表_数据推送(sub);
});
console.log(表_数据);
}
将jQuery dom选择代码从$('table tbody tr')更改为$('row,tr')。每个(function(row,tr)
都可以工作。因为在html中存在两个表dom元素。
函数addproduct()
{
var barcode=$(“#barcode”).val();
var pname=$(“#pname”).val();
var pro#u price=$(“#pro#u price”).val();
变量数量=$(“#数量”).val();
var总成本=$(“#总成本”).val();
var markup=“”+条形码+“”+pname+“”+专业价格+“”+数量+“”+总成本+“”;
$(“#产品列表体”)。附加(标记);
}
函数save(){
var表_数据=[];
$(“#产品列表tbody tr”)。每个(功能(行,tr)
{
var sub={
'条形码':$(tr).find('td:eq(0)').text(),
'pname':$(tr).find('td:eq(1)').text(),
pro_price:$(tr).find('td:eq(2)').text(),
'数量':$(tr).find('td:eq(3)').text(),
‘总成本’:$(tr).find('td:eq(4)').text(),
};
表_数据推送(sub);
});
console.log(表_数据);
}
添加产品
产品代码
品名
价格
数量
数量
选项
添加
产品
去除
产品代码
品名
单价
数量
数量
保存
尝试用ID而不是find方法分配数据。先生。你能写出来吗?然后我就能理解plsvar sub={'barcode':$(“#barcode”)。text(),…}
尝试这样做。如果你使用的是php,那么在表单中使用带有POST方法的提交按钮,并在php文件中获取POST数据$\u POST[“barcode”]
好的,谢谢,先生。我会尝试。我已更改,但结果相同0: