Failing_Solutions Posted June 15, 2011 Share Posted June 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239388-javascript-to-check-how-many-checkboxes-selected-works-outside-of-php-array-but/ Share on other sites More sharing options...
KevinM1 Posted June 15, 2011 Share Posted June 15, 2011 You need to use array notation: document.forms["formName"].elements["checkbox[]"] Quote Link to comment https://forums.phpfreaks.com/topic/239388-javascript-to-check-how-many-checkboxes-selected-works-outside-of-php-array-but/#findComment-1229852 Share on other sites More sharing options...
Failing_Solutions Posted June 15, 2011 Author Share Posted June 15, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/239388-javascript-to-check-how-many-checkboxes-selected-works-outside-of-php-array-but/#findComment-1229854 Share on other sites More sharing options...
KevinM1 Posted June 15, 2011 Share Posted June 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239388-javascript-to-check-how-many-checkboxes-selected-works-outside-of-php-array-but/#findComment-1229864 Share on other sites More sharing options...
Failing_Solutions Posted June 15, 2011 Author Share Posted June 15, 2011 Wonderfull, Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/239388-javascript-to-check-how-many-checkboxes-selected-works-outside-of-php-array-but/#findComment-1229865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.