How to limit chekbox selection if i have more checkbox field
Posted: Fri Aug 30, 2013 6:54 pm
I have 5 deferent checbox groups and each have abaout 30 or more checkboxes, they are created dynamicly. In each group i want to limit the selection on 2. Who pick more then 2 something need to tell him or other checkboxes need to freeze.
I have 5 forms, each have one checkbox group and submit button to save checked stuff int database.
All my forms look like this:
<form action="<?php echo JURI::current(); ?>" method="POST" onsubmit="return check(this)" >';
And my checkboxes are:
echo '<input type="checkbox" name="togglepf[]" value="'.$idpf.'" />';
only names and vale are changed in other 4.. toggle, togglesf[],togglesg[],toggle[f[],toglec[], $idpf,$idpg...
I used this script:
<script>
function check(obj){
len=obj.length;
cnt=0;
max=2;
for(i=0; i<len; i++){
if(obj.type=="checkbox" && obj.checked){
cnt++;
}
}
if (cnt > max){
alert ("Please check only three!!!");
return false;
}
else {
return true;
}
}
I put this script in each form but it counts selected checkboxes on the whole page and not on the one form. So if i pick 2 checkboxes on one form and 2 on another it will not let me to save neither of those.
U must understand that i am a noob in PHP, so if u know the answer beter give me examle or something
Thanks in advance
I have 5 forms, each have one checkbox group and submit button to save checked stuff int database.
All my forms look like this:
<form action="<?php echo JURI::current(); ?>" method="POST" onsubmit="return check(this)" >';
And my checkboxes are:
echo '<input type="checkbox" name="togglepf[]" value="'.$idpf.'" />';
only names and vale are changed in other 4.. toggle, togglesf[],togglesg[],toggle[f[],toglec[], $idpf,$idpg...
I used this script:
<script>
function check(obj){
len=obj.length;
cnt=0;
max=2;
for(i=0; i<len; i++){
if(obj.type=="checkbox" && obj.checked){
cnt++;
}
}
if (cnt > max){
alert ("Please check only three!!!");
return false;
}
else {
return true;
}
}
I put this script in each form but it counts selected checkboxes on the whole page and not on the one form. So if i pick 2 checkboxes on one form and 2 on another it will not let me to save neither of those.
U must understand that i am a noob in PHP, so if u know the answer beter give me examle or something