Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with checkbox array

Status
Not open for further replies.

electricphp

Programmer
Feb 20, 2008
71
US
I have a script that looks like this:

Code:
function calcFees(theCheckbox,reset){
var c = 0;
  for (var i=0; i < document.forms['form1'].elements[theCheckbox].length; i++){
  if (document.forms['form1'].elements[theCheckbox][i].checked){c = c + 1}
  }

and it basically counts the number of checkboxes that are checked on a page.

the checkboxes are generate dynamically and look like this
Code:
<input type="checkbox" value="" name="water" onclick="calcFees('water');">
<input type="checkbox" value="" name="water" onclick="calcFees('water');">
<input type="checkbox" value="" name="water" onclick="calcFees('water');">
...

the problem is that I need them like this, as an array for further processing
Code:
<input type="checkbox" value="" name="water[]" onclick="calcFees('water');">
<input type="checkbox" value="" name="water[]" onclick="calcFees('water');">
<input type="checkbox" value="" name="water[]" onclick="calcFees('water');">
...
but then the script doesn't work anymore.

any suggestions?
 
what exactly are you trying to do with the array?

Looks like you are processing something as soon as each one is checked

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
when a checkbox is checked, the javascript calculate how many boxes are checked and multiplies it by a fee, its a calculator.
 
then what do you want to do with the array?

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
I want to submit it to a php page which will dump it into a database. Do you understand what I'm asking you? What difference does it make what I'm doing with the array?
 
I'm just trying to figure out if you need an array...

It sounds like you are just trying to get the checkboxes that are checked and pass each of those values to a PHP page.

If that is true, then what you have won't do anything as none of your checkboxes have a value assigned to it.

If you do this:

<input type="checkbox" name="mycbx" value="1" />
<input type="checkbox" name="mycbx" value="2" />
<input type="checkbox" name="mycbx" value="3" />

When submitted, a $_GET or $_POST to "mycbx" will return the one that was checked (it's value). If multiple ones are checked, it will return each as a comma separated value. (i.e. 1,3 if the 1st and second one was checked).

Basic HTML here, no rocket science involved.




TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
^ 1st and 3rd one - sorry

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
not exactly, if multiple ones are selected, only the last one gets sent to the php page because they all have the same name,

name="mycbx" which goes back to my original point, if I want the values comma separated need them sent as an array, meaning name="mycbox[]"

but by using the brackets it screws up the function[]

and the checkboxes have different values
 
I figured out what I needed to do in case someone else stumbles upon this post:

aElem = form1.elements["water[]"];
if (aElem.name.substr(0,4)=="water" ) { ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top