0

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

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by rajeesh_rsn
1

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! :)

0

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']?>);" />
0

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

Once again Thanks
Rajeesh

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.