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?
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?
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>
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...
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"> </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']))); ?>
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; xwhich 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...