Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



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

Posts: 10
Trades: 0
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
 
 
Register now for full access!
Old 01-23-2006, 11:28 PM
vangogh's Avatar
Post Impressionist

Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
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 |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-24-2006, 03:41 PM
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
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 03:43 PM..
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 01-24-2006, 08:24 PM
vangogh's Avatar
Post Impressionist

Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
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 |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-26-2006, 11:00 AM
ibbo's Avatar
Super Spam Talker

Posts: 880
Location: Leeds UK
Trades: 0
"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
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

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, 03:29 PM
vangogh's Avatar
Post Impressionist

Posts: 10,688
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
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 |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 01-29-2006, 08:29 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
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';

Please login or register to view this content. Registration is FREE
(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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.51184 seconds with 12 queries