How to limit checkbox selection if i have more checkbox groups on one page.

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:

[php]’;
[/php]
And my checkboxes are:
[php]echo ‘’;
[/php]
only names and values are changed in other 4… toggle, togglesf[],togglesg[],toggle[f[],toglec[], $idpf,$idpg…

I used this script:

[php]
function check(obj){
len=obj.length;
cnt=0;
max=2;
for(i=0; i<len; i++){
if(obj[i].type==“checkbox” && obj[i].checked){
cnt++;
}
}
if (cnt > max){
alert (“Please check only three!!!”);
return false;
}
else {
return true;
}
}
[/php]
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.

I tryed with
if(obj[i].type==“checkbox” && obj[i].checked && (obj[i].name==“toggle…” )

I change name of funcion, name of obj but then the script doesnt working.

U must understand that i am a noob in PHP, so if u know the answer beter give me examle or something :slight_smile: Thanks in advance

wrong forum, that is a JavaScript not a PHP Problem.

but off topic:
simple solution just reset cnt eveytime obj.name canges:
for { if(obj.name!=lastname) cnt=0; … ; lastname=obj.name }

Sponsor our Newsletter | Privacy Policy | Terms of Service