Hi, Friends.
Here is the easiest way to select multiple checkboxes and then also reading their values and make our task.
The below code is to read the checkbox array. Its self explanatory.
<?php
if(isset($_POST['submit'])) {
if(isset($_POST['check_list'])) {
$fields = $_POST['check_list'];
if (is_array($fields)) {
echo "<pre>";
print_r($fields);
echo "</pre>";
foreach ($fields as $key=>$val) {
echo "$key -> $val ";
}
$content = '<hr>';
$content = $content . count($fields);
for ($i=0;$i<count($fields);$i++) {
$content = $content . "<li>$fields[$i]\n";
}
echo $content;
}
}
}
?>
In the below HTML and JAVASCRIPT code, nice work has been done. In JavaScript first we are finding the "check_list" array and then assigning it to out array variable and then selecting multiple checkboxes.
<html>
<head>
<title>CheckAll and UnCheckAll</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script LANGUAGE="JavaScript">
function CheckAll()
{
var a=new Array();
a=document.getElementsByName("check_list[]");
//alert("Length:"+a.length);
/*var p=0;
for(i=0;i<a.length;i++){
if(a[i].checked){
alert(a[i].value);
p=1;
}
}*/
for (i = 0; i < a.length; i++)
a[i].checked = true ;
}
function UnCheckAll(chk)
{
var a=new Array();
chk=document.getElementsByName("check_list[]");
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
</script>
</head>
<body>
<form name="myform" action="checkbox1.php" method="post">
<b>Scripts for Web design and programming</b>
<input type="checkbox" name="check_list[]" value="1">ASP
<input type="checkbox" name="check_list[]" value="2">PHP
<input type="checkbox" name="check_list[]" value="3">JavaScript
<input type="checkbox" name="check_list[]" value="4">HTML
<input type="checkbox" name="check_list[]" value="5">MySQL
<input type="button" name="Check_All" value="Check All" onClick="CheckAll();">
<input type="button" name="Un_CheckAll" value="Uncheck All" onClick="UnCheckAll();">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Try it. It works nice.
Cheer. :)