Php 复选框和jquery ajax

Php 复选框和jquery ajax,php,jquery,ajax,checkbox,Php,Jquery,Ajax,Checkbox,我正在尝试创建一个模块,其中每当选中复选框时,数据库中指示的价格将添加到总费用中,并显示结果。如何从数据库SQL/插入php代码中获取值并显示所选框的结果 <script> function updateTextArea() { var allVals = []; $('#c_b :checked').each(function() { allVals.push($(this).val()); }); $('#t').val(allV

我正在尝试创建一个模块,其中每当选中复选框时,数据库中指示的价格将添加到总费用中,并显示结果。如何从数据库SQL/插入php代码中获取值并显示所选框的结果

<script>
function updateTextArea() { 
    var allVals = [];
    $('#c_b :checked').each(function() {
        allVals.push($(this).val());
    });
    $('#t').val(allVals)
}
$(function() {
    $('#c_b input').click(updateTextArea);
    updateTextArea();
});
index.php

$queryPaymentLab = "select * from requestedlab where patient_ID='6' AND paid='0'";
$resultPaymentLab = mysql_query($queryPaymentLab);
$countRowsPaymentLab=mysql_num_rows($resultPaymentLab);

if($countRowsPaymentLab > 0){
while($fetchLab=mysql_fetch_array($resultPaymentLab)){
    $labservice_ID=$fetchLab['labservice_ID'];

    $queryPrice = "select * from labservices where labservice_ID='".$labservice_ID."'";
    $resultPrice = mysql_query($queryPrice);
    $fetchPrice=mysql_fetch_array($resultPrice);

    $labService=$fetchPrice['labService'];
    $labPrice=$fetchPrice['labPrice'];
    $totalLab+=$labPrice;
    $totalHBill+=$labPrice;

    echo "<tr><td><input type='checkbox' name='labservice_ID' value='$labservice_ID'/></td>
    <td>Laboratory</td><td>$labService</td><td>$totalHBill</td></tr>";
}
}
显示总费用

 $queryPrice = "select * from labservices where labservice_ID='".$labservice_ID."'";
    $resultPrice = mysql_query($queryPrice);

    while($fetchPrice=mysql_fetch_array($resultPrice)){   
        $labPrice=$fetchPrice['labPrice'];
        $total+=$labPrice;
    }

    echo "<strong>Total:</strong>
    <strong>$total</strong>";

<>你为什么不考虑使用HTML5数据属性?

HTML:

在onchange事件中为每个复选框调用updateCost 确保取消选中复选框会适当地从总价中减去价格! 别忘了在提交时汇总并检查价格,否则你很容易欺诈性购买! 这样可以避免不必要的Ajax,代码保持干净。抱歉,如果这不是你想要的,请告诉我,以便我能更好地帮助你

如果你在某个市场上,成本本身正在实时更新,这就需要一些外部帮助。对于市场,只需调用Ajax函数即可适当地更新每个商品的数据价格属性!:

<?php 
// Use PHP to dynamically fetch the data-price values and generate the HTML below
?>
<span class="item" data-price="100">...</span>
<span class="item" data-price="90">...</span>
<span class="item" data-price="130">...</span>

USD: <span class="price">0</span>
function updateCost() {
    $('.item').each(
        function() {
            if ($(this).children('input:checkbox').is(':checked')) {
                var oldPrice = parseInt($('.price').text());
                var newPrice = oldPrice + parseInt($(this).data('price'));
                $('.price').text(newPrice);
            }
        }
    );
 }