Jump to content

Javascript to check how many checkboxes selected works outside of php array but.


Recommended Posts

I 'm trying to make sure up to 10 checkboxes out of 80 are selected.

 

I have working code if the checkboxes are not in an array but I can't seem to get them to work inside an array. I hope this is something simple.

 

This code works if I name the checkboxes are just named checkbox but I can't put them in a PHP array without checkbox[] and if I do that it breaks the javascript. What do I do?

<script Language="JavaScript">
<!--
function checkbox_checker()
{
var checkbox_choices = 0;
for (counter = 0; counter < form.checkbox.length; counter++)
{
if (form.checkbox[counter].checked)
{ checkbox_choices = checkbox_choices + 1; }
}
if (checkbox_choices > 10 )
{
msg="You may only Play 10 numbers.\n"
msg=msg + "You have made " + checkbox_choices + " selections.\n"
msg=msg + "Please remove " + (checkbox_choices-10) + " selection(s)."
alert(msg)
return (false);
}
return (true);
}
-->
</script>

 

I am generating 80 checkboxes with a loop then I need to use those values as variables so I think I need to put them into an array by using [] in the name

This is the checkboxes being generated.

<?php 
  for ($i=1; $i<=80; $i++) {
  echo "<input type='checkbox' name='checkbox[]' id='chk$i' value='$i' style='visibility: hidden';>";
  }
  ?>

 

When the form is submitted I need to make sure no more then 10 checkboxes are selected, but the javascript code above doesn't seem to like me using [] in it, and \[\] doesn't seem to escape the characters.

 

Hope that makes since. I can' get the values with PHP by using checkbox[] but then I can't validate the amount with javascript. Or I can remove the [] and validate but I then can't get the $_POST values with PHP.

 

Any help is appreciated.

Thank you,

 

So

for (counter = 0; counter < form.checkbox.length; counter++)

becomes

for (counter = 0; counter < document.forms["form"].elements["checkbox[]"]; counter++)

 

But how do I deal with this?

if (form.checkbox[counter].checked)

 

Like this?

if (document.forms["form"].elements["checkbox[]"[counter]].checked)

 

Your best bet is to stick the complicate to write data in variables, like so:

 

var checkboxLength = document.forms["formName"].elements["checkbox[]"].length;
var checkboxes = document.forms["formName"].elements["checkbox[]"];


for(var i = 0; i < checkboxLength; ++i) {
   if(checkboxes[i].checked) {
      // do stuff
   }
}

 

You not only make it easier to write doing it this way, you also make it more efficient in the long run as you store the checkboxes in a variable before the loop rather than walking down the DOM to obtain a new reference on each iteration.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.