views:

86

answers:

4

I have web page in PHP which displays all records in a table. I want to add check boxes against all rows and user can check a check box to check the checkboxes in each generated row..

how can I do this?

+1  A: 

You must use javascript. Add:

<script type="text/javascript">
var check=document.getElementById("The id of the checkbox"), 
table=document.getElementById("The id of the table");

check.onclick=function(){
var ck=table.getElementsByTagName("input");
for(i=0;i<ck.length;i++) if(ck[i].type=="checkbox") ck[i].checked=true;
}
</script>
mck89
This will work, however it will check all the checkboxes on a form. If there are others as well, comparing the name-attribute would be needed.
nikc
No you will check only checkboxes in the table. But i agree with you that insert an attribute filter is better.
mck89
still doesnt work..I'm out of ideas already..plus, is this format a violation?<form> <table> <tr> <td> <input 'checkbox/> </td> </tr> <tr> <td> <input 'checkbox/> </td> </tr> </table></form>
A: 

One way that I've used in the past is to define the checkbox id's and use that.

Here's a sample that I wrote up that works for me...

<?php
  $num_chkboxes = 3;
?>
<html>
  <head>
    <script type="text/javascript">
      function chk_all()
      {
        for (i = 1; i <= <?php echo $num_chkboxes; ?>; i++)
        {
          document.getElementById('chk'+i).checked = true;
        }
      }
    </script>
  </head>
  <body>
    check all <input type="checkbox" name="chkall" id="chkall" onclick="chk_all();" /><br /><br />

    <table>
      <tr>
        <td>
          chk1<input type="checkbox" name="chk1" id="chk1" />
        </td>
      </tr>
      <tr>
        <td>
          chk2<input type="checkbox" name="chk2" id="chk2" />
        </td>
      </tr>
      <tr>
        <td>
          chk3<input type="checkbox" name="chk3" id="chk3" />
        </td>
      </tr>
    </table>
  </body>
</html>

It would be pretty simple from then on to add a little logic to make it a check/uncheck all box...

espais
A: 

I cant use this code...

my code goes like this..

  <table id="top" width=100% border="1" bordercolor="#000000" cellpadding="0" cellspacing="0" style="border-style:solid; border-width:0.5px;">
  <form id="myForm" name="myForm" action="" method="post">
    <tr style="background-color:#C0C0C0;">          
      <td width="155" align="center"><b>Product Group</b></td>
      <td width="318" align="center"><b>Group Name</b></td>
      <td width="310" align="center"><b>PIC</b></td>          
      <td align="center" colspan="2">&nbsp;</td>  
      <td width="21" bordercolor="#000000" style="border-style:solid; border-width:1px;">
        <input type="button" onclick="SetAllCheckBoxes('myForm', 'myCheckbox', true);" value="I like them all!">
        <input type="checkbox" id="checkall" name="ca_v1_on" value="Check All myCB" onclick="checkAll(1);"/>
       <input type="checkbox" name="ca_v1_off" value="Uncheck All myCB" onclick="checkAll(0);"/>
        <!-- ?php echo $form->checkbox('done'); ? -->
      </td>        
    </tr>

echo $form->end(); foreach($prodGroupData as $arr) { ?>
link('Edit', array('controller' => 'productMasters','action' => 'editGroup','?' => array( 'prodGroupNo' => $arr['ProductGroup']['PROD_GROUP_NO']))); ?> link('Delete', array('controller' => 'productMasters','action' => 'deleteGroup','?' => array( 'prodGroupNo' => $arr['ProductGroup']['PROD_GROUP_NO']))); ?>

        <input type="checkbox" name="myCheckbox" value="2" id="myCheckbox2">
      </td>
    </tr>

Pages [ << ] [ < ] [ > ] [ >> ] link('Delete Selected Items', array('controller' => 'productMasters','action' => 'deleteGroup','?' => array( 'prodGroupNo' => $arr['ProductGroup']['PROD_GROUP_NO']))); ?>

Out of curiosity, why are your checkboxes outside of the form?
espais
no they are not..they are far though.. (my bad)that is why I'm asking if the format is valid..<table><form><tr><td>checkbox</td></tr><tr><td>checkbox</td></tr></form></table>I'm new to web dev and am trying to learn by myself..pls help..
The form should go outside of the table declaration, ie <form><table>...</table></form>What I still don't understand though, from your post above, is that you have echo $form->end(); foreach (..){...}Are you sure the </form> isn't coming before all of your elements?
espais
A: 

to be honest, i'm doing cakephp... and im totally lost around it..

here is a cleaner version: create('ProductGroup',array('url' => array('controller' => 'productMasters', 'action' => 'addGroup'))); ?> td.in a, td.in a:visited{ color: #0000FF; }


  submit('Search',array('controller'=>'productMasters','action'=>'searchGroup')); ?> submit('Add',array('controller'=>'productMasters','action'=>'addGroup')); ?>
Product Group Group Name PIC
 

checkbox('checkAll',array('onClick' => 'CheckAll(1)')); ?>
link('Edit', array('controller' => 'productMasters','action' => 'editGroup','?' => array( 'prodGroupNo' => $arr['ProductGroup']['PROD_GROUP_NO']))); ?> link('Delete', array('controller' => 'productMasters','action' => 'deleteGroup','?' => array( 'prodGroupNo' => $arr['ProductGroup']['PROD_GROUP_NO']))); ?>
checkbox('check'); ?--> Pages [ << ] [ < ] [ > ] [ >> ] link('Delete Selected Items', array('controller' => 'productMasters','action' => 'deleteGroup','?' => array( 'prodGroupNo' => $arr['ProductGroup']['PROD_GROUP_NO']))); ?>

  <br />

end(); ?>

the javascript i use is

function CheckAll(flag) { alert(document.getElementById('check').length); if ( document.ProductGroupAddForm.check.length ) { for (var x = 0; x

which is at a different file (a layout file)

...

the entire code is a mess of mixed php, html, and cakephp its really wearing me out but i dont want to give up...