Hai friends I need a simple (?) advice in one of my web page.

I had developed a web application in one of its page there are many number of checkboxes. Users are need to select single or multiple check boxes. But there is a limit for each users. ie If an user are only permitted to select 2 items, then I need to disable all the rest of checkboxes after he/she selected the 2. And also they can select another item , if they deselect the existing item.

Friends I am little bit confused and wondered how to do this. I am using PHP for development.

Please help me.
Thanks for your valuable time
Rajeesh

Recommended Answers

All 3 Replies

Very simple.

<html>
<head>
<script type="text/javascript">
function checkCheckbox() {
 var countselection = 0;
 var maximumselection = 2;
 var ch = document.getElementsByName('choice');
 for(i=0;i< ch.length;i++) {
  if (ch[i].checked) {
   countselection++;
  }
 }
 for(i=0; i < ch.length;i++) {
  if (!ch[i].checked) {
	if(countselection == maximumselection) {
  		ch[i].disabled=true;
	} else {
		ch[i].disabled=false;
	}
  }
 }
}
</script>
</head>
<body>
<form name="form">
1 <input type="checkbox" name="choice" value="1" onclick="checkCheckbox();" /><br />
2 <input type="checkbox" name="choice" value="2" onclick="checkCheckbox();" /><br />
3 <input type="checkbox" name="choice" value="3" onclick="checkCheckbox();" /><br />
4 <input type="checkbox" name="choice" value="4" onclick="checkCheckbox();" /><br />
5 <input type="checkbox" name="choice" value="5" onclick="checkCheckbox();" /><br />
6 <input type="checkbox" name="choice" value="6" onclick="checkCheckbox();" /><br />
</form>
</body>
</html>

Cheers! :)

as the allowed checkboxes are different and limited for different users, I guess you are storing that information inside the user's details table.
and while you fetch other details for the user , get that count too.

$sql_query = "select username,userid,chkbox_limit,age from usermaster";

You can pass it to the checkCheckbox() function to achieve what you want.

<input type="checkbox" name="choice" value="1" onclick="checkCheckbox(<?php echo $row['chkbox_limit']?>);" />

Thank you for your kind reply. Let me go through your advices...

Once again Thanks
Rajeesh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.