Select all check box using one check
box
Here we are going to create a page that contains 6 check boxes.
And 2 other checkbox which we use for selects all and deselect all.
Now open your favorite PHP text editor and type following code.
Here I am going to create a webpage that contain 6 Language.
<body>
<p>Pls Select languages you know;</p>
<form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
print"<pre>";
print"<input type='checkbox' name='lang[]' value='C'>C<br>";
print"<input type='checkbox' name='lang[]' value='c++'>C++<br>";
print"<input type='checkbox' name='lang[]' value='Java'>JAVA<br>";
print"<input type='checkbox' name='lang[]' value='V.B.NET'>V.B.NET<br>";
print"<input type='checkbox' name='lang[]' value='ASP.NET'>ASP.NET<br>";
print"<input type='checkbox' name='lang[]' value='c sharp'>C Sharp<br>" ;
print"<input type=checkbox name='ckeckall' value='ckeckall' onclick='checkall(checkall)'>Select
All<br>";
print"<input type=checkbox name='ckeckall1' value='ckeckall1'
onclick='uncheck(uncheck)'>UncheckALL<br>";
print"</pre>";
?>
</form>
Save this file as index.php.
· Here our design Part has finished.
Now we are going to add javascript for select all checkboxes using one check box .
Add following code to index.php in between <head></head> head tag.
<head>
<script language="javascript">
function checkall(chkall) // Function for select all check box.
{
var chk=form1.elements;
for(var i=0;i<chk.length;i++)
{
if(chk[i].type="checkbox")
{
if(chkall.checked=true)
chk[i].checked=true;
else
chk[i].checked=false;
}
}
} // finished
function uncheck(uc) //Function for deselect all
{
var chk1=form1.elements;
for(var i=0;i<chk1.length;i++)
{
if(chk1[i].type="checkbox")
{
if(uncheck.checked=true)
chk1[i].checked=false;
else
chk1.checked=true;
}
}
} //finished
</script>
</head>
Now save this file.
Open your favorite browser and run this file.