How to limit the number of checkboxes to be selected using php???

Recommended Answers

All 11 Replies

Using php?? OR using js?
Post your code.

Checkboxes are limited via HTML.. They'll be already limited when they appear in the page you create.

suppose i am posting 5 checkboxes and i want users to select any 3 of them then how to do it???

can you please post your code here?

You need JS. If you're using jQuery, you could do something like this (if not, it's the same thing but it would take quite a bit more coding):

$('input:checkbox').click(function(){
  $('input:checkbox:not(:checked)').attr('disabled', $('input:checkbox:checked').length >= 3 ? 'disabled' : '');
});

my code is too long its about 327 checkboxes in different section i need to limit the selection in all of the sections

its simply lyk i hav putd some checkboxs and nothing and is using php for other things and nothing else

Well, you could also count the amount of selected boxes with php and echo a warning if it's > 3, of course, but personally I'd definitely use JS for this.

Try this code It using Java script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
function check()
{
var len=document.f.ck.length;
var ck=document.f.ck;
var l=0;
for(var i=0;i<len;i++)
{
if(ck[i].checked==true)
{
l++;
}
if(l>3)
{
ck[i].checked = false ;
}
}



}
</script>
</head>

<body>
<form id="form1" name="f" method="post" action="">
  <p>
    <input name="ck" type="checkbox" id="ck" value="checkbox" onclick="check()"/> 
  PHP</p>
  <p>
    <input name="ck" type="checkbox" id="ck" value="checkbox" onclick="check()"/> 
  JSP</p>
  <p>
    <input name="ck" type="checkbox" id="ck" value="checkbox" onclick="check()"/> 
  JAVA</p>
  <p>
    <input name="ck" type="checkbox" id="ck" value="checkbox" onclick="check()"/> 
  CSS</p>
  <p>
    <input name="ck" type="checkbox" id="ck" value="checkbox" onclick="check()" /> 
    HTML</p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
</body>
</html>

I think it's more user friendly to disable the checkboxes, but well. That's a possibility too.

Using PHP I dnt have any idea.

Thanx 4 ur help I m done wid this

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.