duanjiu4498 2014-04-26 20:34
浏览 32
已采纳

AJAX发布到PHP,计算总和并返回

I got a table in html, I send a POST in ajax with 2 arrays, one called rowValues that contains 3 arrays, one for every row. And another array called columnValues that contains 3 arrays one for every column.

I POST it ok, but the problem is : How can I calculate in PHP the sum of every row and every column, and do an echo to success function showing the result in class "totalCol" and "total" of every column and row?

This is my code:

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


<script>

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

     var columnValues={}, rowValues={};

     $("#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 CODE:

foreach ($_POST['rows'] as $rowNumber => $values){
    // $values is an array with all the values in the row
    echo "Hola";
}
foreach ($_POST['columns'] as $columnNumber => $values){
    // $values is an array with all the values in the column
}

Thank You in Advance Guys!

  • 写回答

1条回答 默认 最新

  • douyan6871 2014-04-26 22:36
    关注

    The PHP code:

    $result = array(
        'rows' => array(), 
        'columns' => array()
    );
    
    foreach ($_POST['rows'] as $rowNumber => $values){
        // $values is an array with all the values in the row
        $result['rows'][$rowNumber] = array_sum($values);
    }
    foreach ($_POST['columns'] as $columnNumber => $values){
        // $values is an array with all the values in the column
        $result['columns'][$columnNumber] = array_sum($values);
    }
    
    // output the result as json
    echo json_encode($result);
    

    And the Javascript success callback:

    function(data){
        var rows = data.rows,
            columns = data.columns;
    
        // insert your server-calculated data to dom   
        $("td.total").each(function(rowIndex){
            $(this).text(rows[rowIndex+1]);
        });
    
        $("tr.totalCol td").each(function(columnIndex){
            $(this).text(columns[columnIndex+1]);
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊