Ver Mensaje Individual
  #5 (permalink)  
Antiguo 20/03/2013, 13:28
cachusan
 
Fecha de Ingreso: septiembre-2011
Mensajes: 219
Antigüedad: 12 años, 7 meses
Puntos: 31
Respuesta: Validar checkbox con php

Ejemplo cortito pero ilustrativo:

Código PHP:
Ver original
  1. <?php
  2.  
  3.   if(@$_POST){
  4.     $checkbox_recibidos = count($_POST['ck']);
  5.     if($checkbox_recibidos > 5){
  6.       echo "selecciono mas de 5";
  7.       // codigo de retorno
  8.     }else{
  9.       echo "seleccion igual o menor a 5";
  10.       // codigo de redireccion
  11.     }
  12.    
  13.     die;
  14.   }
  15. ?>
  16.  
  17. <html>
  18. <head>
  19.  
  20.   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  21.   <script type="text/javascript">
  22.  
  23.   jQuery(function(){
  24.       var max = 5;
  25.       var checkboxes = $('input[type="checkbox"]');
  26.  
  27.       checkboxes.change(function(){
  28.           var current = checkboxes.filter(':checked').length;
  29.           checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
  30.       });
  31.   });
  32.  
  33.  
  34.   </script>
  35.  
  36. </head>
  37. <body>
  38.   <form name="test" id="test" action="" method="post">
  39.     <input type="checkbox" name="ck[]" value="1">1
  40.     <input type="checkbox" name="ck[]" value="2">2
  41.     <input type="checkbox" name="ck[]" value="3">3
  42.     <input type="checkbox" name="ck[]" value="4">4
  43.     <input type="checkbox" name="ck[]" value="5">5
  44.     <input type="checkbox" name="ck[]" value="6">6
  45.     <input type="checkbox" name="ck[]" value="7">7
  46.     <input type="checkbox" name="ck[]" value="8">8
  47.     <input type="checkbox" name="ck[]" value="9">9
  48.  
  49.     <input type="submit"  value="enviar" />
  50.   </form>
  51. </body>
  52. </html>