Reply
Setting values of array checkboxes
Old 01-23-2006, 09:32 PM Setting values of array checkboxes
Novice Talker

Posts: 10
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?
benhorton2 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 01-23-2006, 10:28 PM
vangogh's Avatar
Post Impressionist

Latest Blog Post:
A Focused Blog Is A Happy Blog
Posts: 8,663
Name: Steven Bradley
Location: Boulder, Colorado
Did you give all your checkboxes a different name.

if you have something like:

<input type="checkbox" name="name1">

then you should be able to reference with document.form.checkbox.name1

All your checkboxes should have a different name so you should be able to reference each one by its name.

Hope that helps and works for you.

__________________
l Search Engine Friendly Web Design | Van SEO Design
l Tips On Marketing, SEO, Design, and Development | TheVanBlog
l Custom WordPress Themes
| Small Business Forum
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-24-2006, 02:41 PM
funkdaddu's Avatar
Web Design Snob

Posts: 636
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 offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 01-24-2006, 07:24 PM
vangogh's Avatar
Post Impressionist

Latest Blog Post:
A Focused Blog Is A Happy Blog
Posts: 8,663
Name: Steven Bradley
Location: Boulder, Colorado
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.
__________________
l Search Engine Friendly Web Design | Van SEO Design
l Tips On Marketing, SEO, Design, and Development | TheVanBlog
l Custom WordPress Themes
| Small Business Forum
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-26-2006, 10:00 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
"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.

Ibbo
__________________
www.nationalclubgolfer.com www.sportspub.co.uk www.bespokecc.co.uk www.centralmarquees.co.uk
Linux user #349545 :
(GNU/Linux)iD8DBQBAzWjX+MZAIjBWXGURAmflAKCntuBbuKCWenpm XoA7LNydllVQOwCf
ibbo is offline
Reply With Quote
View Public Profile Visit ibbo's homepage!
 
Old 01-26-2006, 02:29 PM
vangogh's Avatar
Post Impressionist

Latest Blog Post:
A Focused Blog Is A Happy Blog
Posts: 8,663
Name: Steven Bradley
Location: Boulder, Colorado
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.
__________________
l Search Engine Friendly Web Design | Van SEO Design
l Tips On Marketing, SEO, Design, and Development | TheVanBlog
l Custom WordPress Themes
| Small Business Forum
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-29-2006, 07:29 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
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)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to Setting values of array checkboxes
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14582 seconds with 13 queries