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.

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.