将Javascript中的复选框求和组合到我的PHP数据库表中

时间:2016-01-26 07:41:40

标签: javascript php checkbox sum html-table

这是我当前数据库填充表的代码。我找到了一个以前的Javascript代码来帮助我从多个复选框中汇总值。

我想要求的值只来自我的Asset_Cost

User myUser = new User();
myUser.Role = "r1,r2,r3,r4,r5";
myUser.UserID = 1;
myUser.Username = "MyUserName";

UserEditViewModel result = Mapper.Map<UserEditViewModel>(myUser);

我想整合此代码中的复选框总和:

<body>

    <table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
        <thead>
            <tr>


                <th><h3>Asset ID</h3></th>
                <th><h3>Vendor</h3></th>
                <th><h3>Hardware </h3></th>
                <th><h3>Cost</h3></th>
                <th><h3>Purchase Date</h3></th>
                <th><h3>Select Cost</h3></th>



            </tr>
        </thead>
        <tbody>

        <?php


    $result = mysql_query("SELECT * FROM Asset");

        while($row = mysql_fetch_array($result)) 
    { 


    echo "<tr>";

    echo "<td>" . $row['Asset_ID']."</td>"; 
    echo "<td>" . $row['Vendor_Name'] . "</td>"; 
    echo "<td>" . $row['Hardware_ID'] . "</td>"; 
    echo "<td>" . $row['Asset_Cost'] . "</td>"; 
    echo "<td>" . $row['DateOfPurchase'] . "</td>";

    echo "</tr>";
 } 

echo "</table>";

这是javascript

<?php
include('connect1.php');
$result = $db->prepare("SELECT * FROM Asset");
        $result->bindParam(':userid', $res);
        $result->execute();
        for($i=0; $row = $result->fetch(); $i++){
    ?>
        <INPUT TYPE="checkbox" NAME="items[]" value="<?php echo $row['Asset_Cost'] ?>" id="sum_m_<?php echo $i ?>" onclick="UpdateCost()">


        <td>

        <?php echo $row['Asset_ID'] ?>
        <?php echo $row['Vendor_Name'] ?>
        <?php echo $row['Hardware_ID'] ?>
        <?php echo $row['Asset_Cost'] ?>



        </td>

        <br>


    <?php
    }
?>
<br>

Total Cost : <input type="text" name="sen" id="totalcost" value="">

1 个答案:

答案 0 :(得分:0)

    <body>

        <table cellpadding="0" cellspacing="0" border="0" id="table" class="sortable">
            <thead>
                <tr>
                    <th><h3>Select</h3></th>
                    <th><h3>Asset ID</h3></th>
                    <th><h3>Vendor</h3></th>
                    <th><h3>Hardware </h3></th>
                    <th><h3>Cost</h3></th>
                    <th><h3>Purchase Date</h3></th>
                </tr>
            </thead>
            <tbody>

            <?php


        $result = mysql_query("SELECT * FROM Asset");

        $calculated = mysql_num_rows($result);

        while($row = mysql_fetch_array($result)) 
        { 


        echo "<tr>";
        echo "<td><INPUT TYPE='checkbox' NAME='items[]' value='". $row['Asset_Cost'] ."' id='sum_m_$i' onclick='UpdateCost()'> </td>";
        echo "<td>" . $row['Asset_ID']."</td>"; 
        echo "<td>" . $row['Vendor_Name'] . "</td>"; 
        echo "<td>" . $row['Hardware_ID'] . "</td>"; 
        echo "<td>" . $row['Asset_Cost'] . "</td>"; 
        echo "<td>" . $row['DateOfPurchase'] . "</td>";
        echo "</tr>\n";


        }
    ?>
    <br>

    Total Cost : <input type="text" name="sen" id="totalcost" value="">


    <script type="text/javascript">

    function UpdateCost() {
      var sum = 0;
      var gn, elem;
      for (i=0; i<<?php echo $calculated ?>; i++) {
        gn = 'sum_m_'+i;
        elem = document.getElementById(gn);
        if (elem.checked == true) { sum += Number(elem.value); }
      }
      document.getElementById('totalcost' ).value = sum.toFixed(0);
    }
    window.onload=UpdateCost();

    </script>



</body>

大多数事情都和你的代码一样。只给$ $计算的值等于mysql结果中的行数。希望这会有所帮助..

相关问题