如何使用ajax更新表中列的总和而不刷新页面

时间:2016-10-11 14:10:55

标签: php ajax

include "config.php";
$output = "";

//deleting part
if(isset($_POST["id"])) {
    foreach($_POST["id"] as $id) {
        $delete = "DELETE FROM sat WHERE id = '".$id."'"; 
        mysqli_query($connection, $delete);
    }
}

//viewing part
$output .= "<table>";

while($row = mysqli_fetch_array($stmt)) {
    $numberof = $numberof + 1;
    $totalcost = $totalcost + $row['third'];

    $output .= '<tr>
                <td>' .$numberof. '</td>
                <td>' .$row['first']. '</td>
                <td>' .$row['second']. '</td>
                <td>' .$row['third']. '</td>
                </tr>';
}
$output .= "<tr>
            <td>Total:</td>
            <td>$totalcost.00</td>
            </tr>
            </table>";

这是我的php页面,其中查看和删除过程都在一个php文件中进行。该过程首先从用户那里获取删除行中任何数据的请求。问题是我需要刷新页面以获得&#34; column = third&#34;的实际总和值。否则它将显示先前的总和值。 Fyi,删除过程将取决于复选框请求。下面的代码显示了ajax部分:

$("#confirmCB").on("click", function () {
        if(confirm("Are you confirm with the selection?")) {
            var id = [];

            $(':checkbox:checked').each(function(i) {
                id[i] = $(this).val();
            });
            if(id.length === 0) {
            }
            else {
                $.ajax({
                    url: 'index.php',
                    method: 'post',
                    data:{id:id},
                    success:function() 
                    {
                        for(var i=0; i<id.length; i++) {
                            $('tr#'+id[i]+'').css('background-color', '#FFFFFF');
                            $('tr#'+id[i]+'').fadeOut('slow');
                        }
                    }
                });
            }
}
        else {
            return false;
        }

});

0 个答案:

没有答案