Ver Mensaje Individual
  #13 (permalink)  
Antiguo 06/07/2011, 13:22
alexg88
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años
Puntos: 344
Respuesta: Problemas con validación de checkbox

La idea es usar el valor del elemento, no la clave.

Revisa el html porque tienes que cambiar cosas para que funciones bien la validación.

Código HTML:
Ver original
  1. <script type="text/javascript">
  2. function valida()
  3. {
  4.  
  5.     inputs = document.getElementsByName("oferta[]");
  6.    
  7.     for (i=0; i<inputs.length; i++)
  8.    {
  9.    
  10.        if (inputs[i].checked)
  11.        {        
  12.    
  13.            return true;
  14.            
  15.        
  16.        }
  17.    
  18.    }
  19.    
  20.    alert("¡No has seleccionado ninguna oferta!");
  21.    return false;
  22. }
  23. </head>
  24. <form action="parametros.php" method="post" onsubmit="return valida()">
  25.  
  26. <input type="checkbox" name="oferta[]" value="C1" />
  27. <input type="checkbox" name="oferta[]" value="C2" />
  28. <input type="checkbox" name="oferta[]" value="C3" />
  29.  
  30. <input type="submit"  />
  31.  
  32. </form>
  33. </body>
  34. </html>

Un ejemplo de como podrías hacer la validación, luego puedes meterlo si quieres en un bucle.

Código PHP:
Ver original
  1. <?php
  2.  
  3.  
  4. if (isset($_POST['oferta'])){
  5.  
  6. if (in_array("C1",$_POST['oferta'],true))
  7.  echo "C1 está en el array";
  8.  
  9.  
  10. }  
  11.  
  12. ?>