Page 1 of 1

How to limit chekbox selection if i have more checkbox field

Posted: Fri Aug 30, 2013 6:54 pm
by bojanf
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

Re: How to limit chekbox selection if i have more checkbox f

Posted: Fri Aug 30, 2013 6:56 pm
by bojanf
My title is not good, i wanted to say How to limit checkbox selection if i have more checkbox groups on one page.

Re: How to limit chekbox selection if i have more checkbox f

Posted: Sat Aug 31, 2013 5:47 pm
by Christopher
You would use Javascript to do that. Register an event function for when a checkbox is checked. Check to see if more than one other checkbox in that set is checked. You could either not allow a new check, or clear the oldest check, etc.

Re: How to limit chekbox selection if i have more checkbox f

Posted: Tue Sep 03, 2013 1:48 am
by sukhchain
try this.

with jquery:

Code: Select all

<form onsubmit="return check(this)">
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
</form>
<form onsubmit="return check(this)">
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
	$('form').each(function(){
		var frm=$(this);
		$(this).find('input[type=checkbox]').click(function(){
			var len=$(frm).find('input[type=checkbox]:checked').length;
			if(len>2)
			{
				
				$(this).attr('checked',false);
				alert('You can only select 2 values from each form');
			//	$(frm).find('input[type=checkbox]').attr('disabled',true);
			}	
		});
	});
});
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;
	}
}
</script>