with this script that limits the number of activated checkboxes :
<script type="text/javascript">
/***********************************************
* Limit number of checked checkboxes script- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more
***********************************************/
function checkboxlimit(checkgroup, limit){
var checkgroup=checkgroup
var limit=limit
for (var i=0; i<checkgroup.length; i++){
checkgroup[i].onclick=function(){
var checkedcount=0
for (var i=0; i<checkgroup.length; i++)
checkedcount+=(checkgroup[i].checked)? 1 : 0
if (checkedcount>limit){
alert("You can only select a maximum of "+limit+" checkboxes")
this.checked=false
}
}
}
}
</script>
After the form is submitted i need to see what was chosen, but if i dump the variables using the following code in order.php :
How can i get to the actual data? I see it must have something to do with the fact that each input currently has the same name but i don’t know how to progress from here.
If the checkboxes all have the same name (but each must have a unique ID, if ID is used, at all), then all checked values are assembled as a comma-delimited list.
So if your checkbox values are the same as the display of country names, and the user checks “USA” and “Japan”, then the post value will be “USA,Japan”.
HTH,
UPDATE: Ah… you didn’t give the checkboxes any VALUE. Give each the value of the country name. (The ‘on’ value just means that the checkboxes exist in the form scope. If no checkboxes were checked, there would be no element named ‘countries’ because an empty checkbox doesn’t exist when the form is submit.)
There might be some kind of delay to make sure the code doesn’t contain anything malicious. (That’s just a guess… I’ve never had a problem with code appearing, before.)
Are you sure that it’s not displaying the two countries with a comma between them? If the names are identical, this should be the case. If there’s only ONE country showing, I’m not sure what could be causing that.
There must be SOMEthing different, here. I know PHP doesn’t process things (fundamentally) any different than any other scripting language. When the form is submit, if you have more than one checkbox checked, that value appears as a comma-delimited list of the checked values of those checkboxes (assuming the checkboxes all have the same name.)
But now my javascript won’t work because it doesn’t expect to be passed an array.
I havent learned JS so am unsure how to modify the script :
function checkboxlimit(checkgroup, limit){
var checkgroup=checkgroup
var limit=limit
for (var i=0; i<checkgroup.length; i++){
checkgroup[i].onclick=function(){
var checkedcount=0
for (var i=0; i<checkgroup.length; i++)
checkedcount+=(checkgroup[i].checked)? 1 : 0
if (checkedcount>limit){
alert("You can only select a maximum of "+limit+" checkboxes")
this.checked=false
}
}
}
}
Also, i can’t see images in the buttons in the question editor (quote whole post, strong etc …), may be my adblocker ?
When the form submits, although the value passed is a comma-delimited list, before it’s posted all checkboxes with the same name can be treated as an array (I think they are collectively an array-like object.) So the array notation for the name shouldn’t be necessary. And, as you’ve discovered, is breaking the validation that you already have in place.
The thing that confuses me about your checkboxlimit function is that it appears as though it’s adding an onclick event to all the checkboxes, but that should already be in place BEFORE the validation is actuated.