0

I have a question regarding a php form. I've added a checkbox I would like it to select all in one click and click it separately.

<form name=myform action="addcanteen.php" method=post>
    <table><tr><td width="11%"> Date:</td><td width="89%"><input type="date" name="cdate" id="cdate"/></td></tr></table>
    <table border="1" bgcolor="#F5F9C1">
    <tr>
    <!--<th>ID</th>-->
    <th>Name</th>
    <td><!--<input type="text" id="datepicker">-->
      <input type="checkbox" id="selectall" name="chk[]"/></td>
    <td align="center">coffee</td>  
    <td align="center">tea</td>
    </tr>
    <?php while($row=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <?php /*?><td><?php echo $row['emp_id'];?></td><?php */?>
    <td><?php echo $row['emp_name'];?></td>

    <td align="center"><input type="checkbox" class="name" name="chk1[]" value="<?php echo $row['emp_name'];?>"/></td>

    <td><input type="text" name="coffee[]" id="coffee" value="" /></td>
    <td><input type="text" name="tea[]" id="tea" value=""/></td>
    </tr>
    <?php } ?>
    <tr>
    <td>
    </td>
    <td>
    </td>
    <td align="center">
    <input type="submit" class="button" name="submit" id="submit" value="submit"/>
    </td>
    </tr>
    </table>
    </form>

here is the php code for the insertion, everything works there except the insertion of single checkbox selection.

<?php
    include(dbcon.php);
    date_default_timezone_set('UTC');
    /*$d=date("D");*/
    $date=date("d");
    $month=date("m");
    $year=date("Y");
    $fd=date("d-m-Y");
    if(isset($_POST['submit']))
    {
        $cdate=$_POST['cdate'];
    //$emp_id=$_POST['emp_id'];
    $checkbox1=$_POST['chk1'];
    $tea=$_POST['tea'];
    $coffee=$_POST['coffee'];
    /*$dd=date("d-m-y");*/
    for ($i=0; $i<sizeof($checkbox1);$i++) 
    {
    $query1="INSERT INTO canteen(name,coffee,tea,date)VALUES('".$checkbox1[$i]."','".$coffee[$i]."','".$tea[$i]."','$cdate')";
    $sql1=mysql_query($query1);
    }
    }
    header("location:canteen.php");
    ?>
Midhun R
  • 11
  • 6
  • where is your html code and what error you got using your code? – Saty Apr 28 '15 at 09:42
  • @saty i commented my problem below...please help me – Midhun R Apr 28 '15 at 09:54
  • you want when you check selectall checkbox all the checkbox inside you while loop is also selected?? – Saty Apr 28 '15 at 09:59
  • @saty thanks for your time...yes. i need that function..when i click select all check box data enters properly..but when i click one separately only text name was inserted..i need to insert the number of coffee and tea with this name.I would be really grateful for your help in advance! – Midhun R Apr 28 '15 at 10:11

1 Answers1

2

I do get your point, But I suggest you to use map function in JavaScript with reference to class. For more info refer this link

Community
  • 1
  • 1
Thyagi
  • 190
  • 1
  • 6
  • 1
    Imagine you have a check box labelled as Select All and on click of that trigger all check boxes to get selected through a function and the write the code for map function as described in the link above, so that the check boxes which are checked, their values will be returned. – Thyagi Apr 28 '15 at 11:08