I have a form that submits via post. I name the checkboxes followed by [] (i.e. <input type=checkbox name="checkbox[]">) so that I use PHP to get the results as an array ($checkbox=$_POST['checkbox[]']).
My question is how can I use Javascript to fill out those checkboxes? I've tried things like document.form.checkbox[0].checked=true; or document.form.checkbox[][0].checked=true; but nothing works. Any ideas?
You'll have to call them by element number: document.forms[0].elements[i].checked=true, as you have repeating input names. I always thought how PHP made you have use the bracketed names was poor.
Last edited by funkdaddu : 01-24-2006 at 02:43 PM.
funkdaddu is right about needing to use the element number. I didn't notice that you were using the same name for all the checkboxes.
I prefer using different names on my checkboxes though so it's easier to remember what's referring to what. I try to use meangful names and ids for things whenever possible.
"I prefer using different names on my checkboxes though so it's easier to remember what's referring to what. I try to use meangful names and ids for things whenever possible."
This depends on how many check boxes there is going to be. If its a registration form say you might find that accessing an html array of checkbox[] is much easier than finding all the names of each seperate checkbox. I shudder even thinking of it.
PHP can easily find out if is_array($_GET['checkboxes']) and foreach it if it is.
That's true. Sometimes the array is definitley easier. Most of the time I don't have too many checkboxes in my forms so with a few I'll give them meaningful names.
You're definitely right though that when you have more than a few an array is the way to go.
I think you can name them checkbox[0] checkbox[1] etc, (fil in the number in the brackets). That way PHP can read them as an array and yet they have unique names for JS to use.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful'; Scribble Pad MOD for phpBB (aka MSN handwriting for forums)