Dalia
Dalia

Reputation: 7

how to add values of checkbox to get the total price

I am creating a page that has some services and their prices. So, when the user choose the service by checking the checkbox next to it, the price should be added to the total price. Also, if he unchecked the checkbox, the price of that service will be subtracted from the total.

I have tried many ways but I could not get the result.

This is my code.. Could anybody help me to do what I want

<html>
    <head>
        <title> new bill</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>



<?php
    include 'config.php';
    include 'searchbill.php';
    $query = "SELECT * FROM service";
    $result = mysqli_query($con, $query);
    if ($result){

            ?>
         <center> <font size="5" face="WildWest" color="#4EE2EC"> Services</font></center>

    <table align="center" class="imagetable" accept-charset="utf-8">
    <tr style="background-color: #80E6CC;color:white;" aligh="center">
        <th>number</th>
        <th>service name</th>
        <th>employee</th>
        <th>price</th>
        <th>check</th>     
        </tr>  
         <?php
         $id = 1;
        while ($row = mysqli_fetch_array($result)){
            echo "<tr><td align='center'>" . $id++;
             echo "<td align='center'>" . $row["sname"] . "</td>";
             echo "<td align='center'>";

        $emb = "SELECT * FROM employee";
        $resultemb = mysqli_query ($con, $emb);
        if($resultemb){?>
            <select name="ename" STYLE="margin-right: 83px; alignment-adjust: middle; font-family: Verdana; font-weight: bold; font-size: 12px; color: #379EA5; " >
            <?php while ($row1 = mysqli_fetch_array($resultemb)){
                for($i=1; $i<= count($row1["ename"]);$i++){
                     $select = $row1["ename"];
                     echo $select;
                     echo "<br/>";
                     echo "<option value='$select'>".$select."</option>";
                 } 
            } ?>
        </select>
            <?php
        } 
        echo "<td align='center'>" . $row["sprice"] . "</td>";
        echo "<td align='center'>";?>
        <input type="checkbox" name="serprice" value="<?php $row['sprice']; ?>">
        <?php
        echo "</td></tr>";

         ?>
       <?php 
        }


                }



?>



    </body>
    </html>

Upvotes: 0

Views: 2382

Answers (2)

Shaunak D
Shaunak D

Reputation: 20646

Try this, Demo Fiddle

$('input[type="checkbox"]').change(function(){
    var totalprice = 0;
    $('input[type="checkbox"]:checked').each(function(){
        totalprice= totalprice + parseInt($(this).val());
    });
    $('#price').val(totalprice);
});

To calculate values of checked inputs, use :checked .

Upvotes: 1

sushil bharwani
sushil bharwani

Reputation: 30197

Try this it should work Here is the jsfiddle for this http://jsfiddle.net/sushilbharwani/QCndj/



$('input[type=checkbox]').click(function(){
total = 0;
$('input[type=checkbox]:checked').each(function(){

total = total + parseInt($(this).val());

});

alert(total);
});

Upvotes: 0

Related Questions