js limits the number of checkbox checks and php's method of getting multiple checkbbox is analyzed in depth

  • 2020-07-21 07:17:05
  • OfStack

The first is the code of js limiting the number of checkbbox checks:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title> Limit the number of check box selections </title> 
<script type="text/javascript" > 
function  checkDate(n){ 
  var checkedCount=0; 
  for(var i=0;i<myForm.course.length ;i ++){ 
  if(myForm.course[i].checked){ 
     checkedCount++; 

      } 
  } 
   if(checkedCount>n){ 

  alert(" Can't choose more than 3 course "); 

       return false; 

  } 
  } 

</script> 
</head> 
<body> 
<form name="myForm" method="post" id="myForm" action="test.php"> 
 Please choose courses for this semester ( most 3 The door ) : <br> 
<input type="checkbox" name="course[]" value=" Chinese language and literature " onClick="return checkDate(3)"> Chinese language and literature <br> 
<input type="checkbox" name="course[]" value=" mathematics " onClick="return checkDate(3)"> mathematics <br> 
<input type="checkbox" name="course[]" value=" English " onClick="return checkDate(3)"> English <br> 
<input type="checkbox" name="course[]" value=" chemical " onClick="return checkDate(3)"> chemical <br> 
<input type="checkbox" name="course[]" value=" physical " onClick="return checkDate(3)"> physical <br> 
<input type="checkbox" name="course[]" value=" political " onClick="return checkDate(3)"> political <br> 
<input type="submit" value=" submit "> 
<input type="reset" value=" cancel "> 
</form> 
</body> 
</html> 

Then there is the method of php file to get checkbbox:

<?php
$course=$_POST['course'];
for($i=0;$i<count($course);$i++) 
{ 
if($course[$i]!="") 
{
echo $course[$i]."<br/>";
} 
} 
?>


Related articles: