gobbles Posted May 22, 2008 Share Posted May 22, 2008 Hey Ya'll I have a bit of a problem with a script im writing and was wondering if someone could help me out. I have a number of checkboxes that are dynamically created using the following: <input type="checkbox" name="we_cater_to[]" value="<?php echo $rowresult['value'];?>" <?php if($plan == 'Free') {?> onclick="chkcontrol(<?php echo $control; ?>)" <?php } else {} ?>> I also have a javascript function that limits the amount of checkboxes clickable to 3 which is as follows: <script type="text/javascript"> function chkcontrol(j) { var total=0; for(var i=0; i < document.createlisting.we_cater_to.length; i++){ if(document.createlisting.we_cater_to[i].checked){ total =total +1;} if(total > 3){ alert("Please Select only three") document.createlisting.we_cater_to[j].checked = false ; return false; } } } </script> The problem im having is with the name of the checkboxes "we_cater_to[]" ... If i remove the [] at the end it all works perfect, except that the checkboxes dont save in the way i want them to ... so i need the [] to make the array ... and if i add [] to the javascript function then the function doesnt work at all. How do i get the javascript to work using the [] ? Any help would be great, i have been racking my brains and cant seem to get a result. Cheers, Link to comment https://forums.phpfreaks.com/topic/106709-checkbox-issue/ Share on other sites More sharing options...
nloding Posted May 22, 2008 Share Posted May 22, 2008 Not really a PHP problem, but I was curious, so I did some searching ... http://forums.devshed.com/javascript-development-115/checkbox-array-javascript-15233.html Sounds like quite the predicament. Good luck. I'd suggest maybe setting a hidden field and have the client-side javascript cycle through the checkboxes and set the value of the hidden field to the values of the checked boxes separated by commas, then explode/split it when it's passed over to the server in the $_POST variable. Link to comment https://forums.phpfreaks.com/topic/106709-checkbox-issue/#findComment-547038 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.