How to limit chekbox selection if i have more checkbox field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bojanf
Forum Newbie
Posts: 2
Joined: Fri Aug 30, 2013 6:37 pm

How to limit chekbox selection if i have more checkbox field

Post 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
bojanf
Forum Newbie
Posts: 2
Joined: Fri Aug 30, 2013 6:37 pm

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
sukhchain
Forum Newbie
Posts: 1
Joined: Tue Sep 03, 2013 1:22 am

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

Post 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>
Post Reply