问题描述
我需要帮助弄清楚如何将表行中的数据提交到PHP中进行处理.我需要它能够提交多行数据.有什么想法吗?
I need help figuring out how to submit data from a table row into PHP to be processed. I would need it to be able to submit multiple rows of data. Any ideas?
我有一个表此处.我希望能够检查该行并将数据提交到php以处理数据.这是一个HTML表.我只想知道如何将变量传递给php,例如数组之类的东西
I have a table here. I want to be able to check the row and submit the data to php to process the data. It is an HTML table. I just want to know how to pass the variables to php, e.g. an array or something
好吧,像这样的东西:
$("submit_btn").click(function () {
$('#diary tbody>tr input:checked').each(function() {
$.post('process.php',
data: "an array",
success: "data submitted");
}
});
如何获取数组中的表行数据以提交? (答案在下面)
How would I get the table row data in an array to submit it? (Answer is below)
确定第2部分:
将表格行数据发送到php.
Sending the table row data to php.
jQuery代码:
rows = JSON.stringify(rows); // submit this using $.post(...)
alert(rows);
$.post('classes/process.php', rows, function(data) {
$('#results').html(data);
})
.error(function() { alert("error"); })
});
PHP代码:
<?php
$rowsArray = json_decode($_POST['rows']);
echo $rowsArray;
?>
错误:
Notice: Undefined index: rows in C:\...\classes\process.php on line 6
推荐答案
$('#submit_btn').click(function(){
var rows = [];
$('#box-table-a tbody tr input[type=checkbox]:checked').each(function(i,v){
var tds = $(v).parents('tr').children('td');
rows.push({
'name': tds.eq(1).find('select').val(),
'units': tds.eq(2).text(),
'calories': tds.eq(3).text(),
'sugar': tds.eq(4).text()
});
});
rows = JSON.stringify(rows); // submit this using $.post(...)
$.post('classes/process.php', {'rows': rows}, function(data){
console.log(data);
});
});
使用$ .post()提交rows
,然后在服务器端,可以使用json_decode()
;
submit rows
using a $.post() and then, in the server side, you can convert back into an array using json_decode()
;
示例输出:
[{名称":水",单位":"1",卡路里":"2",糖":"3"},{名称":食品",单位" :"4",卡路里":"5",糖":"6"}]
[{"name":"Water","units":"1","calories":"2","sugar":"3"},{"name":"Food","units":"4","calories":"5","sugar":"6"}]
演示:
http://jsfiddle.net/cp6ne/84/
这篇关于将HTML表格行提交给php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!