I have a group of checkboxes using this syntax:
echo "<form name=\"myform\" method=\"POST\" action=\"csvbuilder.php\">";
echo "<input type=\"checkbox\" name=\"list[]\" value=\"".$res['id']."\" />";
At the bottom I have a “Check all” button..
echo "<input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.myform.list)\" />
Which refers to the following JS script..
function checkAll(field) {
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}
Here’s the problem:
The check all button only works when I remove the square brackets from the input name, ie name="list" as opposed to name="list[]". I need the brackets, or else the PHP form handler won’t work. I have tried adding the brackets to the JS, ie onClick="uncheckAll(document.myform.list[])" but that doesn’t work either.
How do I get JS and PHP to play nicely?
Thanks to all responders. I especially appreciate any references/explanations/jfiddles.
It can be accessed with
document.myform['list[]']: