hi. i'm seeking help with checkboxes.i have built a ordering system. i have two tables "food" and "refreshments" both consists of columns "name,price" . what i want to do is . if i click on my submit button , all the checked checkboxes should be added up and have a grand total. my problem that i have now is that. when i submit all the prices are added up , but it should only add the checked ones. please help me.

here is how my tables look like

food        
food_id   name         price
1         bunny chow    15
2         chicken       56

refreshments
refresh_id  name    price
1           coke    13
2          fanta    15

and here is what i have done. "orders.php"

<?php
require_once('mySQLConnect.php');

if(isset($_REQUEST['placeOrder'])) {

        $sql2 = " select sum(price) as totalamount from food ";
        $q = mysqli_query($conn,$sql2);
        $row2 = mysqli_fetch_array($q);
        $total = $row2['totalamount'];

        echo $total;
}
?>

<html>
    <head>
        <title>
            orders   
        </title>
            <link rel="stylesheet" href="joomla.css"> 
                <script src="jquery-1.7.1.min.js"></script>
                <script src="packaged.min.js"></script>
                <script src="jquery-ui-1.8.17.min.js"></script>
                <script type="text/javascript" src="overlib_mini.js"></script>
                <script type="text/javascript" src="Placeholders.js"></script>

    <style>
        input[type="number"] {
            width:50px;
        }

        input[type="checkbox"] {
            width:100px;
            height:20px;
        }

        #buttonCancel {
            background : url("img/Cancel.png") no-repeat center center;
            height :48px;
            width : 48px;
            border : none;
            color : transparent;
            font-size : 0;
            white-space: nowrap;
            display:inline  

        }   

        #buttonAccept {
            background : url("img/Check.png") no-repeat center center;
            height :48px;
            width : 48px;
            border : none;
            color : transparent;
            font-size : 0;
            white-space: nowrap;
            display:inline  

        }   
        </style>
    </head>

    <body>
        <form name="form1" id="form1" method="post">

            <div id="dialog-form" style="border: 3px; border-bottom-color: #877d78">
            </div>
        <table border="0" cellspacing="5" cellpadding="5" width="90%">
        <tbody>
        <tr>
        <td>
    <table class="table_title"  style="background-color: #71bf44;"  border="0" cellspacing="10" cellpadding="0" width="100%">
        <tbody>
         <tr>
            <td align="CENTER" valign="middle">ORDER Menu</td>
             <td align="CENTER" valign="middle"><input type="submit" id="buttonAccept" name="placeOrder"><input type="reset" id=                          "buttonCancel" name="confirm"></td>
          </tr>
       </tbody>
  </table>

          <table border="0" cellspacing="5" cellpadding="5" width="90%">
         <tbody>
      <tr>
        <td>
        <table class="table_title"  style=" background-color:#666;""  border="0" cellspacing="10" cellpadding="0" width="25%">

            <tbody>

             <tr>
                <td align="CENTER" valign="middle">food</td><td><br>


              </tr>


            </tbody>
          </table>
   <table border="0" cellspacing="5" cellpadding="5" width="90%">
    <tbody>
      <tr>

        <td>
<table>
    <thead  style="background-color: #71bf44; color: white;" border="0" cellspacing="1" cellpadding="1" width="100%">

            <th width="2%" height="5">Item name</th>
            <th width="2%" height="5">Price</th>
            <th width="2%" height="5">Quantity</th>
            <th width="2%" height="5">Select item</th>

          </thead>

          <tbody>

           <?php

                    $sql = "Select * from food";
                    $rs = mysqli_query($conn,$sql);

          while ($row = mysqli_fetch_assoc($rs)) {
               $k=('1');
              if ($k==1) {
                $k=0;
                echo'<tr style="background-color: #d5e6d9" height=30px>';  

            }else{
                $k=1;
                echo '<tr height=30px>';
            }



                echo '<td width="2%" height="5">'. $row['name']. '</a></td> '; 

                echo '<td width="2%" height="5">R'. $row['price'].'</td>';
                echo '<td width="2%" height="5"><input type="number"   name="quantity"></td>';
                echo '<td width="2%" height="5"><input type="checkbox"></td>';


          echo '</tr>';
                        }

          ?>      

          </tbody>    
          </table>
             <table border="0" cellspacing="5" cellpadding="5" width="90%">
    <tbody>
      <tr>
        <td>
        <table class="table_title"  style="background-color:#666;"  border="0" cellspacing="10" cellpadding="0" width="25%">
            <tbody>
             <tr>
                <td align="CENTER" valign="middle">refreshments</td><br>

              </tr>


            </tbody>
          </table>
          <table border="0" cellspacing="5" cellpadding="5" width="90%">
    <tbody>
      <tr>

        <td>
<table>
                <thead  style="background-color: #71bf44; color: white;" border="0" cellspacing="1" cellpadding="1" width="100%">

            <th width="2%" height="5">Item name</th>
            <th width="2%" height="5">Price</th>
            <th width="2%" height="5">Quantity</th>
            <th width="2%" height="5">Select item</th>

          </thead>

          <tbody>
            <?php
                $sql = "Select * from refreshments";
                $rs = mysqli_query($conn,$sql);

              while ($row = mysqli_fetch_assoc($rs)) {
                $k=('1');
                if ($k==1) {
                $k=0;
                echo'<tr style="background-color: #d5e6d9" height=30px>';  
                }else{
                $k=1;
                echo '<tr height=30px>';
                }
                echo '<td width="2%" height="5">' . $row['name']. '</a></td> '; 
                echo '<td width="2%" height="5">R'. $row['price'].'</td>';
                echo '<td width="2%" height="5"><input type="number"   name="quantity"></td>';
                echo '<td width="2%" height="5"><input type="checkbox"></td>';
              echo '</tr>';
                        }

          ?>    

    </form>
   </body>
</html>

Recommended Answers

All 3 Replies

when i submit all the prices are added up , but it should only add the checked ones

This is because select sum(price) as totalamount from food selects from all the rows you have in the table. To get only the checked items you have to read them from the $_POST array after form is submitted. In order to have checkboxes registered in the $_POST array, you have to give them the name attribute. I suggest that each checkbox has the item's ID in the name so you will know which items were checked. Also you will want to take into account quantities to be able to get the total price. Something like:

// input for the quantity (fq + ID)
echo '<td width="2%" height="5"><input type="number" name="fq' . $row['food_id'] . '" value="1"></td>';
// checkbox for the food item (fi + ID)
echo '<td width="2%" height="5"><input type="checkbox" name="fi' . $row['food_id'] . '"></td>';

Now the $_POST array will contain all the checked items and their respecting quantities. With a bit of juggling you can get an array of the IDs of the items selected and construct a query:

$sql2 = "select sum(price) as totalamount from food WHERE food_id IN (" . implode(',', $itemsIDArray) . ')';

But as I said, you might want to take quantities into account.

just what i needed . thanx alot. i knew i had to take that into account as my my price will be influenced by the quantity but i didnt know how to put that in code. thanx alot . much appreciated

Great. If you have troubles getting useful data out of the POST, let us know, we will help. Otherwise, if the problem has been solved please mark this thread as solved. Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.