JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 4.67 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 24th, 2002, 11:16 AM
robpet robpet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 45 robpet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
How to use Javascript to set state of checkboxes defined as PHP array?

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.

Reply With Quote
  #2  
Old February 24th, 2002, 01:25 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
you can use the "for ... in ..." construct or access the checkboxes by their number (at least in IE) like this:

document.checkboxes['same_name'][0]

(untested, maybe document.checkboxes['same_name[]'][0] )

Reply With Quote
  #3  
Old February 25th, 2002, 11:10 AM
robpet robpet is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 45 robpet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Solution Found

After posting my question, I found various other solutions posted elsewhere. Apparently lots of people run into this problem of accessing the PHP checkbox array with JS. For others looking for a solution to this problem, don't look under the heading HTML/Javascript, look under the heading PHP. I found lots of solutions at www.phpbuilder.com/forum/ in their PHP forum.

Lots of people presented quite complicated solutions. The simplest solution I found was a little script that works well for me. Here's the script:

Quote:
<script language="JavaScript">
<!--

function UncheckAll() {
document.form_name.reset();
}

function CheckAll() {
with (document.form_name) {

for (var i=0; i < elements.length; i++) {
if (elements[i].type == 'checkbox' && elements[i].name == 'checkbox_name[]')
elements[i].checked = true;
}
}
}

//-->
</script>



Note: This script is for the situation in which all the checkboxes are given the same name using the [] so that PHP recognizes you are creating an array. For example:

Quote:
<input type="checkbox" name="checkbox_name[]" value="value1">
<input type="checkbox" name="checkbox_name[]" value="value2">

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How to use Javascript to set state of checkboxes defined as PHP array?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap