hi all i have this project to do b ut i have having a bit of a problem with checkbox, only selected checkbox goes to the database. it works but only the first value which is chips is inserted even if chapati is selected.
below is the html code:

<?php session_start();
include("conn.php");
    if(isset($_SESSION['id'])){
        $query="SELECT name, course FROM details where id like '".$_SESSION['id']."'";
        $result_user=mysql_query($query,$conn);
        $id=$_SESSION['id'];
        $name=mysql_result($result_user,0,0);
        $course=mysql_result($result_user,0,1);
 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

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

<title>OSHWAL COLLEGE CAFETERIA - ORDER</title>

<meta name="keywords" content="restaurant company, free website template, CSS, HTML, templatemo.com" />

<meta name="description" content="restaurant company - free website template, HTML CSS layout" />

<link href="templatemo_style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#apDiv1 {
    position: absolute;
    left: 303px;
    top: 453px;
    width: 13px;
    height: 159px;
    z-index: 1;
}
</style>
<script type="text/javascript" src="reflection.js"></script>
<script>
function showPrice(str)
{
if (str=="")
  {
  document.getElementById("txtFoodPrice").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtFoodPrice").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getfoodprice.php?foodtype="+str,true);
xmlhttp.send();
}

function showDrinkPrice(str)
{
if (str=="")
  {
  document.getElementById("txtDrinkPrice").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtDrinkPrice").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getdrinkprice.php?d="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>
<div id="templatemo_container">

  <div class="templatemo_topmenu">

    <ul>
      <li><a href="index.php">MAIN PAGE</a></li>
      <li></li>
      <li><a href="order.php" class="current">ORDER</a></li>
      <li></li>
      <li><a href="gallery.html">GALLERY</a></li>
      <?php
      if(isset($_SESSION['id'])){
      ?>
      <li><a href="logout.php">Logout</a></li>
       <?php
      }
      ?>
    </ul>

  </div>

  <div id="templatemo_topsection">OSHWAL COLLEGE CAFETERIA</div>

  <div id="templatemo_headersection">
    <table width="805" height="170" border="0">
      <tr>
        <td width="267" height="164"><img src="images/chikkudukaaya-indian-broad-bean-stir-fry-with-chapati.jpg" width="250" height="200" /></td>
        <td width="266"><img src="images/Chili Paneer.jpg" width="250" height="200" /></td>
        <td width="250"><img src="images/chips.jpeg" width="250" height="200" /></td>
      </tr>
    </table>
  </div>
  <div id="templatemo_content_section">
    <div>
      <table width="150" border="0">
        <tr>
          <td width="144"><strong>Personal Details</strong></td>
        </tr>
        <tr>
          <td height="26">
            <p>
              <label for="id"><strong>ID
                  <br />
              </strong></label>
              <input name="id" type="text" id="id" value="<?php echo $id; ?>" readonly="readonly" />
            </p>
          </td>
        </tr>
        <tr>
          <td height="24">
            <p>
              <label for="name"><strong>Name
                  <br />
              </strong></label>
              <input name="name" type="text" id="name" value="<?php echo $name; ?>" readonly="readonly" />
              </p>
          </td>
        </tr>
        <tr>
          <td height="24">
            <p>
              <label for="course"><strong>Course
                  <br />
              </strong></label>
              <input name="course" type="text" id="course" value="<?php echo $course; ?>" readonly="readonly" />
            </p>
          </td>
        </tr>
      </table>
      <table width="200" border="1">
        <tr>
          <td><script type="text/javascript">
function tot(elem) {
var d=document.getElementById("total").value;
var total=Number(d); 
var e=document.getElementById("vat5").value;
var vat5=Number(e); 
var f=document.getElementById("vat12_5").value;
var vat12_5=Number(f); 
var g=document.getElementById("cash_discount").value;
var cash_discount=Number(g); 

var h=(total+vat5+vat12_5)-cash_discount;
document.getElementById("grand_total").value = h;
}

var total = 0;
function getValues() {
var qty = 0;
var rate = 0;
var obj = document.getElementsByTagName("input");
      for(var i=0; i<obj.length; i++){
         if(obj[i].name == "qty[]"){var qty = obj[i].value;}
         if(obj[i].name == "rate[]"){var rate = obj[i].value;}
         if(obj[i].name == "amt[]"){
                if(qty > 0 && rate > 0){obj[i].value = qty*rate;total+=(obj[i].value*1);}
                        else{obj[i].value = 0;total+=(obj[i].value*1);}
                }
             }
        document.getElementById("total").value = total*1;
        total=0;
}

</script>
<script type="text/javascript">
function addRow(tableID) {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var colCount = table.rows[0].cells.length;
            for(var i=0; i<colCount; i++) {
                var newcell = row.insertCell(i);
                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }
            }
        }

                function deleteRow(tableID)
{
            try
                 {
                var table = document.getElementById(tableID);
                var rowCount = table.rows.length;
                    for(var i=0; i<rowCount; i++)
                        {
                        var row = table.rows[i];
                        var chkbox = row.cells[0].childNodes[0];
                        if (null != chkbox && true == chkbox.checked)
                            {
                            if (rowCount <= 1)
                                {
                                alert("Cannot delete all the rows.");
                                break;
                                }
                            table.deleteRow(i);
                            rowCount--;
                            i--;
                            }
                        }
                    } catch(e)
                        {
                        alert(e);
                        }
   getValues();
}
</script>
<body>
<form action="ord.php" method="post" enctype="multipart/form-data" name="gr" onSubmit="return validateForm(this)">
<tr>
<td class="forhead" style="white-space:nowrap;">&nbsp;</td>
</tr>
<tr>
  <table border="0" id="dataTable" width="38%" align="center" cellpadding="0" cellspacing="0" class="normal-text">
    <tr>
      <td class="forhead" style="white-space:nowrap;">&nbsp;</td>
      <td class="forhead" style="white-space:nowrap;">Food Type</td>
      <td class="forhead" style="white-space:nowrap;"><span class="forhead" style="white-space:nowrap;">Qty</span></td>
      <td class="forhead" style="white-space:nowrap;"><span class="forhead" style="white-space:nowrap;">Rate</span></td>
      <td class="forhead" style="white-space:nowrap;"><span class="forhead" style="white-space:nowrap;">Total</span></td>
      <td align="right" class="forhead" style="white-space:nowrap;">&nbsp;</td>
      <td class="forhead" style="white-space:nowrap;">&nbsp;</td>
    </tr>

    <tr>
<td class="forhead" style="white-space:nowrap;" width="20"><input type="checkbox" name="chk[]"/></td>
<td class="forhead" style="white-space:nowrap;" width="80"><label for="textfield"></label>
  <input name="food[]" type="text" id="textfield" value="Chips" readonly="readonly" /></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input type="text"  name="qty[]"  onkeyup="getValues()" style="width:80px;" onBlur=""></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input  name="rate[]" type="text" style="width:80px;" onKeyUp="getValues()" value="60" readonly="readonly"></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input  name="amt[]" type="text" style="width:80px;" 
onKeyUp="getValues()" readonly="readonly"></td>
<td width="80" align="right" class="forhead" style="white-space:nowrap;">&nbsp;</td>

<td class="forhead" style="white-space:nowrap;" width="80">&nbsp;</td>
</tr>
    <tr>
<td class="forhead" style="white-space:nowrap;" width="20"><input type="checkbox" name="chk[]"/></td>
<td class="forhead" style="white-space:nowrap;" width="80"><label for="textfield"></label>
  <input name="food[]" type="text" id="textfield" value="Chapati" readonly="readonly" /></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input type="text"  name="qty[]"  onkeyup="getValues()" style="width:80px;" onBlur=""></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input  name="rate[]" type="text" style="width:80px;" onKeyUp="getValues()" value="20" readonly="readonly"></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input  name="amt[]" type="text" style="width:80px;" 
onKeyUp="getValues()" readonly="readonly"></td>
<td width="80" align="right" class="forhead" style="white-space:nowrap;">&nbsp;</td>

<td class="forhead" style="white-space:nowrap;" width="80">&nbsp;</td>
</tr>

    <tr>
<td class="forhead" style="white-space:nowrap;" width="20"><input type="checkbox" name="chk[]"/></td>
<td class="forhead" style="white-space:nowrap;" width="80"><label for="textfield"></label>
  <input name="food[]" type="text" id="textfield" value="Paneer" readonly="readonly" /></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input type="text"  name="qty[]"  onkeyup="getValues()" style="width:80px;" onBlur=""></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input  name="rate[]" type="text" style="width:80px;" onKeyUp="getValues()" value="150" readonly="readonly"></td>
<td class="forhead" style="white-space:nowrap;" width="80"><input  name="amt[]" type="text" style="width:80px;" 
onKeyUp="getValues()" readonly="readonly"></td>
<td width="80" align="right" class="forhead" style="white-space:nowrap;">&nbsp;</td>

<td class="forhead" style="white-space:nowrap;" width="80">&nbsp;</td>
</tr>

</table>
</tr>
<p>
<input type="hidden" name="order" value="order" />
  <input type="submit" name="button" id="button" value="Submit" />
</p>
<tr>
  </td>
</tr>
</form>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
      </table>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <tr>
    </div>
  </div>
  <div id="templatemo_footer">COPY RIGHT OSHWAL COLLEGE CAFETERIA</div>
</div>
</body>
</html>
<?php
}else{
        header('location:index.php');   
    }
?>

and below is the php code:

<?php
$host='localhost';
$db_name='order';
$user='root';
$pass='';
$conn=mysql_connect ($host, $user, $pass);
mysql_select_db($db_name,$conn);

$checkbox=$_POST['chk'];
$food=$_POST['food'];
$qty=$_POST['qty'];
$rate=$_POST['rate'];
$amt=$_POST['amt'];


if(isset($_POST["order"]))
{
    for ($i=0; $i<sizeof($checkbox);$i++){
        $sql="INSERT INTO order_details (food, qty, rate, amt) VALUES ('".$food[$i]."', '".$qty[$i]."', '".$rate[$i]."', '".$amt[$i]."')";
        mysql_query ($sql);
    }
    echo 'ins';
}
?>

any hep will be appriciated plz help.

Member Avatar for Zagga

From skimming over your rather long code, it looks like you aren't setting any values for the checkboxes.

Have a look at this recent tutorial that has some good information about checkboxes.

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.