duanque2413 2014-04-26 13:23
浏览 50
已采纳

JQuery Array发布到PHP

I post with Ajax 3 arrays, each one for every column. I calculate the sum in PHP, then I echo it in every .totalCol child. I want to post only one array called data, that contains the 3 columns, calculate the sum and echo back, and the same with rows.I want to make it dynamic because when I click "+" button of table It will increment rows and columns And I want to calculate it too.

This is How I calculate the columns:

HTML TABLE:

<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>

POST OF COLUMNS:

$(document).on('change',function(){

       var col1 = [];
       var col2 = [];
       var col3 = [];

       // collect all data from table col1
       $.each($('table td input.sum1'), function(k, v){
           col1.push($(v).val());
       });

       // collect all data from table col2
       $.each($('table td input.sum2'), function(k, v){
           col2.push($(v).val());
       });

       // collect all data from table col3
       $.each($('table td input.sum3'), function(k, v){
           col3.push($(v).val());
       });

       // send data to server
       $.ajax({
           url: 'suma.php',
           type: 'post',
           data: {'col1': col1, 'col2': col2, 'col3': col3,},
           dataType: 'json',
           success: function(data){

               // insert your server-calculated data to dom
               $('.totalCol td:nth-child(1)').text(data.SumCol1);
               $('.totalCol td:nth-child(2)').text(data.SumCol2);
               $('.totalCol td:nth-child(3)').text(data.SumCol3);
           }
       }); 
});

PHP CALC:

<?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;
}
?>

Thank You in advance!

  • 写回答

1条回答 默认 最新

  • dongzhuang6417 2014-04-26 13:38
    关注
    $(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
               }
           }); 
    
    });
    

    In the PHP you'd access them like this:

    <?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
    }
    ?>
    

    About arrays in $.ajax: Objects sent by $.ajax becomes arrays in php. No matter the level.

     $.ajax({
         url: 'arraytest.php',
         data: {
             hello: {
                 foo: 'bar'
             },
             world: {
                 life: 'isgreat'
             }
         }
     });
    

    arraytest.php:

    <?php
    echo $_POST['hello']['foo']; // outputs bar
    echo $_POST['world']['life']; // outputs isgreat
    
    ?> 
    

    If you want to calculate the columns with jQuery/javascript,

    $(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());
           });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条