首页 > 开发 > PHP > 正文

获取checkbox值的php代码

2024-05-04 21:58:22
字体:大 中 小
来源:转载
供稿:网友

由于checkbox特殊性所以我们要获取它的值,在很我语言中都不一样的,在php中我们是以数组形式来处理,并且利用遍历他的值,下面有大量实例.

  1. <html> 
  2. <head> 
  3. <title>checkbox demo</title> 
  4. </head> 
  5. <body> 
  6. <h1>checkbox demo</h1> 
  7.  
  8. <h3>demonstrates checkboxes</h3> 
  9. <form action ="handleformcheckbox.php"> 
  10.  
  11. <ul> 
  12.   <li><input type ="checkbox" name ="chkfries" value ="11.00">fries</li> 
  13.   <li><input type ="checkbox" name ="chksoda"  value ="12.85">soda</li> 
  14.   <li><input type ="checkbox" name ="chkshake" value ="1.30">shake</li> 
  15.   <li><input type ="checkbox" name ="chkketchup" value =".05">ketchup</li> 
  16. </ul> 
  17. <input type ="submit"> 
  18. </form> 
  19.  
  20. </body> 
  21. </html> 

handleformcheckbox.php

  1. <html> 
  2. <head> 
  3. <title>checkbox demo</title> 
  4. </head> 
  5. <body> 
  6. <h3>demonstrates reading checkboxes</h3> 
  7. <?php 
  8. print <<<here 
  9. chkfries: $chkfries <br> 
  10. chksoda: $chksoda <br> 
  11. chkshake: $chkshake <br> 
  12. chkketchup: $chkketchup <br> 
  13. <hr> 
  14.  
  15. here; 
  16.  
  17. $total = 0; 
  18.  
  19. if (!emptyempty($chkfries)){ 
  20.   print ("you chose fries <br>"); 
  21.   $total = $total + $chkfries; 
  22. } 
  23.  
  24. if (!emptyempty($chksoda)){ 
  25.   print ("you chose soda <br>"); 
  26.   $total = $total + $chksoda; 
  27. } 
  28.  
  29. if (!emptyempty($chkshake)){ 
  30.   print ("you chose shake <br>"); 
  31.   $total = $total + $chkshake; 
  32. } 
  33.  
  34. if (!emptyempty($chkketchup)){ 
  35.   print ("you chose ketchup <br>"); 
  36.   $total = $total + $chkketchup; 
  37. } 
  38.  
  39. print "the total cost is $$total"; 
  40.  
  41. ?> 
  42. </body> 
  43. </html> 

实例:

  1. <html> 
  2. <head> 
  3.     <title>using default checkbox values</title> 
  4. </head> 
  5. <body> 
  6. <?php 
  7. $food = $_get[food]; 
  8. $self = htmlentities($_server['php_self']); 
  9. if (!emptyempty($food)) { 
  10.     echo "the foods selected are:<br />"; 
  11.     foreach($food as $foodstuf) 
  12.     { 
  13.         echo "<strong>".htmlentities($foodstuf)."</strong><br />"; 
  14.     } 
  15. } 
  16. else 
  17. { 
  18.     echo ("<form action="$self" "); 
  19.     echo ('method="get"> 
  20.     <fieldset> 
  21.         <label>italian <input type="checkbox" name="food[]" value="italian" /> 
  22. </label> 
  23.         <label>mexican <input type="checkbox" name="food[]" value="mexican" /> 
  24. </label> 
  25.         <label>chinese <input type="checkbox" name="food[]" value="chinese" 
  26.         checked="checked" /></label> 
  27.     </fieldset> 
  28.     <input type="submit" value="go!" >'); 
  29. } 
  30. ?> 
  31. </body> 
  32. </html> 

多选checkbox:

  1. <?php 
  2. $options = array('option 1', 'option 2', 'option 3'); 
  3.  
  4. $valid = true; 
  5. if (is_array($_get['input'])) { 
  6.     $valid = true; 
  7.     foreach($_get['input'] as $input) { 
  8.         if (!in_array($input, $options)) { 
  9.             $valid = false; 
  10.         } 
  11.     } 
  12.     if ($valid) { 
  13.         //process input 
  14.     } 
  15. } 
  16. ?> 

实例checkbox多值获取:

  1. <html> 
  2. <head> 
  3.     <title>using default checkbox values</title> 
  4. </head> 
  5. <body> 
  6. <?php 
  7. $food = $_get["food"];//www.Vevb.com 
  8. if (!emptyempty($food)){ 
  9.     echo "the foods selected are: <strong>"; 
  10.     foreach($food as $foodstuff){ 
  11.         echo '<br />'.htmlentities($foodstuff); 
  12.     } 
  13.     echo "</strong>."; 
  14. } 
  15. else { 
  16.     echo (' 
  17.     <form action="'. htmlentities($_server["php_self"]).'" method="get"> 
  18.         <fieldset> 
  19.             <label> 
  20.                 italian 
  21.                 <input type="checkbox" name="food[]" value="italian" /> 
  22.             </label> 
  23.             <label> 
  24.                 mexican 
  25.                 <input type="checkbox" name="food[]" value="mexican" /> 
  26.             </label> 
  27.             <label> 
  28.                 chinese 
  29.                 <input type="checkbox" name="food[]" value="chinese" checked="checked" /> 
  30.             </label> 
  31.         </fieldset> 
  32.         <input type="submit" value="go!" /> 
  33.     </form> '); 
  34.     } 
  35. ?> 
  36. </body> 
  37. </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表