I wanted to find a way to get multiple checkboxes which I could check or uncheck but with an additional “check all” box that would either do or undo them all.
This is a fairly standard javascript thing. Simply add the following script to the header:
<head>
....
<script LANGUAGE="JavaScript">
<!-- begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = \"true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
// end-->
</script>
....
</head>
then add the form
<form name="myform" action="" method=post>
<input name="mycheckboxarray" type="checkbox" value="0"/>Item 0
<input name="mycheckboxarray" type="checkbox" value="1"/>Item 1
<input name="mycheckboxarray" type="checkbox" value="2"/>Item 2
<input name="mycheckboxarray" type="checkbox" value="3"/>Item 3
<input name="mycheckboxarray" type="checkbox" value="4"/>Item 4
<input name="checkall" type="checkbox"
onclick="this.value=check(this.form.mycheckboxarray)" />Check All
<input name="sub" type="submit" value="Submit"/>
</form>
At the CGI end mycheckboxarray appears as an array filled with the values that are checked. UNLESS you are using PHP!
PHP seems to be a real pain in this area. It requires the form looks like this:
<form name="myform" action="" method=post>
<input name="mycheckboxarray[]" type="checkbox" value="0"/>Item 0
<input name="mycheckboxarray[]" type="checkbox" value="1"/>Item 1
<input name="mycheckboxarray[]" type="checkbox" value="2"/>Item 2
<input name="mycheckboxarray[]" type="checkbox" value="3"/>Item 3
<input name="mycheckboxarray[]" type="checkbox" value="4"/>Item 4
<input name="checkall" type="checkbox"
onclick="this.value=check(this.form.mycheckboxarray)" />Check All
<input name="sub" type="submit" value="Submit"/>
</form>
Which is pretty crap since the Javascript now doesn’t work. Solution? Try the following:
<form name="myform" action="use.php" method=post>
<input name="mycheckboxarray0" type="checkbox" value="0"/>Item 0
<input name="mycheckboxarray1" type="checkbox" value="1"/>Item 1
<input name="mycheckboxarray2" type="checkbox" value="2"/>Item 2
<input name="mycheckboxarray3" type="checkbox" value="3"/>Item 3
<input name="mycheckboxarray4" type="checkbox" value="4"/>Item 4
<input name="checkall" type="checkbox"
onclick="this.value=check(this.form,4)" />Check All
<input name="count" type="hidden" value="5"/>
<input name="sub" type="submit" value="Submit"/>
</form>
and change the Javascript to the following:
var checkflag = "false";
function check(form,count) {
if (checkflag == "false") {
for (i = 0; i < count; i++) {
field = eval('form.mycheckboxarray'+i);
field.checked = true;
}
checkflag = "true";
return "Uncheck All";
}else {
for (i = 0; i < count; i++)
{
field = eval('form.mycheckboxarray'+i);
field.checked = false;
}
checkflag = "false";
return "Check All";
}
}
The PHP code ( use.phph here) can then pick up the count as _POST["count"] and you can get each value thus:
for ($i=0;$i < _POST["count"];$i++)
{
if(_POST["mycheckboxarray$i"])
$checked[] = $i;
}
and bingo you have the same thing as ASP will give you for half the money. Its a fag but it works. The moral is: PHP is crap at multple check checkboxes as it requires a special syntax. Crap really.
The html can be demonstrated here.
