JQuery数组发布到PHP
我用Ajax发布了3个数组,每个列对应一个数组。我用PHP计算总和,然后在每个.totalCol子级中回显它。我只想发布一个名为data的数组,它包含3列,计算总和和回显,行也是如此。我想使它成为动态的,因为当我单击表的“+”按钮时,它将增加行和列,我也想计算它 这是我计算列的方式: HTML表格:JQuery数组发布到PHP,php,jquery,ajax,arrays,Php,Jquery,Ajax,Arrays,我用Ajax发布了3个数组,每个列对应一个数组。我用PHP计算总和,然后在每个.totalCol子级中回显它。我只想发布一个名为data的数组,它包含3列,计算总和和回显,行也是如此。我想使它成为动态的,因为当我单击表的“+”按钮时,它将增加行和列,我也想计算它 这是我计算列的方式: HTML表格: <table id="sum_table"> <tr> <td><input value="0" class="sum1" />
<table id="sum_table">
<tr>
<td><input value="0" class="sum1" /></td>
<td><input value="0" class="sum2"/></td>
<td><input value="0" class="sum3"/></td>
<td class="total">0</td>
</tr>
<tr>
<td><input value="0" class="sum1"/></td>
<td><input value="0" class="sum2"/></td>
<td><input value="0" class="sum3"/></td>
<td class="total">0</td>
</tr>
<tr>
<td><input value="0" class="sum1"/></td>
<td><input value="0" class="sum2"/></td>
<td><input value="0" class="sum3"/></td>
<td class="total">0</td>
</tr>
<tr class ="totalCol">
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<button id="tabla">+</button>
<button id="moes">Hide/Show</button>
PHP计算:
<?php
$SumCol1 = _sumUp($_POST['col1']);
$SumCol2 = _sumUp($_POST['col2']);
$SumCol3 = _sumUp($_POST['col3']);
echo json_encode(array(
"SumCol1" => $SumCol1,
"SumCol2" => $SumCol2,
"SumCol3" => $SumCol3
));
function _sumUp($data)
{
$sum = 0;
foreach($data as $k => $v)
{
$sum += $v;
}
return $sum;
}
?>
提前谢谢你
$(document).on('change',function(){
var columnValues={}, rowValues={};
// columnValues will look like this:
// columnValues={columnNumber:[value, value, value]}
// rowValues the same:
// rowValues={rowNumber:[value, value, value]}
// First, iterate through the rows
$("#sum_table tr").each(function(rowIndex){
$("td input", $(this)).each(function(colIndex){
var value=$(this).val();
// indexes need +1 to get the row number, because
// the indexes are 0-based.
if (undefined===columnValues[colIndex+1]){
columnValues[colIndex+1]=[];
}
if (undefined===rowValues[rowIndex+1]){
rowValues[rowIndex+1]=[];
}
rowValues[rowIndex+1].push(value);
columnValues[colIndex+1].push(value);
});
});
// send data to server
$.ajax({
url: 'suma.php',
type: 'post',
data: {rows:rowValues, columns:columnValues},
dataType: 'json',
success: function(data){
// insert your server-calculated data to dom
}
});
});
在PHP中,您可以这样访问它们:
<?php
foreach ($_POST['rows'] as $rowNumber => $values){
// $values is an array with all the values in the row
}
foreach ($_POST['columns'] as $columnNumber => $values){
// $values is an array with all the values in the column
}
?>
arraytest.php:
<?php
echo $_POST['hello']['foo']; // outputs bar
echo $_POST['world']['life']; // outputs isgreat
?>
我还没有详细阅读,但这看起来可能会有所帮助。。。我应该替换var col1=[];var col2=[];var col3=[];其中:var col1sum=0;var col2sum=0;var col3sum=0;?这只是为了指出区别。不同之处在于
parseInt()
函数。它将求和列值中的字符串转换为整数,这样您就可以对它们进行数学运算:)“只能发送一个数组,其中包含每列的3个数组。计算列,另一个数组包含每行的3个数组并计算行?”正如您在我的代码中看到的,我用Ajax发布了3列,我用PHP计算总和,然后在每个.totalCol子级中回显它。我只想发布一个名为data的数组,它包含3列,计算总和和回显,行也是如此。我想使它成为动态的,因为当我单击表的“+”按钮时,它将增加行和列,我也想计算它。很抱歉,我的解释不好:PNo prob。但请把你给我的解释添加到问题中,以便其他人在遇到类似问题时能够理解:)
$.ajax({
url: 'arraytest.php',
data: {
hello: {
foo: 'bar'
},
world: {
life: 'isgreat'
}
}
});
<?php
echo $_POST['hello']['foo']; // outputs bar
echo $_POST['world']['life']; // outputs isgreat
?>
$(document).on('change',function(){
var col1sum = 0;
var col2sum = 0;
var col3sum = 0;
// collect all data from table col1
$.each($('table td input.sum1'), function(k, v){
col1sum+=parseInt($(v).val());
});
// collect all data from table col2
$.each($('table td input.sum2'), function(k, v){
col2sum+=parseInt($(v).val());
});
// collect all data from table col3
$.each($('table td input.sum3'), function(k, v){
col3sum+=parseInt($(v).val());
});
});