I'm dynamically creating a group of checkboxes. The values are passed to a second page where they are processed by PHP. I want to use Javascript to check all boxes or uncheck all boxes. The form has the name checkboxes:
Quote:
| <form name="form" action="process_checkboxes.php3"> |
If I were to define the checkboxes using different names without putting them into a PHP array, then I know how to use Javascript to check or uncheck all the boxes. For example, I could create the checkboxes thusly:
Quote:
<input type="checkbox" name="checkbox1" value="some_value1">
<input type="checkbox" name="checkbox2" value="some_value2"> |
I could then check all the checkboxes with a function that would contain:
Quote:
document.form.checkbox1.checked = true;
document.form.checkbox2.checked = true; |
I could also give the checkboxes the same name and Javascript automatically creates an array, identifying each checkbox with an index it generates. Here are checkboxes with the same name:
Quote:
<input type="checkbox" name="same_name" value="some_value1">
<input type="checkbox" name="same_name" value="some_value2"> |
This would generate a JS array named "same_name", the elements of which could be accessed using the indices [0] and [1].
I could check both these boxes with a function containing
Quote:
document.checkboxes.same_name[0].checked = true;
document.checkboxes.same_name[1].checked = true; |
So far, so good. Here's the problem. In order to have PHP on the form-processing page successfully process the checkboxes with the same name, they must be named using the PHP convention using "[]" in the name. Using this convention the boxes would be named as follows:
Quote:
<input type="checkbox" name="same_name[]" value="some_value1">
<input type="checkbox" name="same_name[]" value="some_value2"> |
Here's the problem. Apparently adding the [] screws up creation of the Javascript array because I can no longer set the checked state. I get a JS error "document.checkboxes.same_name.0 is not an object". Apparently the JS array value is not created. I would appreciate any pointers as to what I'm doing wrong or for another solution. Thanks very much.