Ver Mensaje Individual
  #10 (permalink)  
Antiguo 29/07/2014, 22:15
Avatar de Triby
Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Mejor forma de validar formuario

Primero que nada: Todas las etiquetas y atributos HTML deben estar en minúsculas.

Un ejemplo con tu código (se me olvidó agregar la altura):
Código PHP:
Ver original
  1. <?php
  2. // Una función útil para crear selects
  3. function html_select($name, $id, $value, $options = array(), $start = 0, $end = 0) {
  4.     echo "<select name=\"$name\" id=\"$id\">\n";
  5.     if(count($options) == 0) {
  6.         // Creamos el array con inicio y fin
  7.         if($start > 0) {
  8.             // Agregamos opción cero
  9.             $options[0] = 'Selecciona...';
  10.         }
  11.         for($i = $start; $i <= $end; $i++) {
  12.             $options[$i] = $i;
  13.         }
  14.     }
  15.     // Agregamos las opciones
  16.     foreach($options as $key => $val) {
  17.         // Para establecer la opción preseleccionada
  18.         $selected = ($key == $value) ? ' selected="selected"' : '';
  19.         echo "\t<option value=\"$key\"$sel>$val</option>\n";
  20.     }
  21.     echo "</select>\n";
  22. }
  23. // Primero los valores iniciales
  24. $edad = 0;
  25. $peso = 0;
  26. $genero = "";
  27. $errores = array();
  28.  
  29. if(isset($_POST['edad'])) {
  30.     $edad = intval($_POST['edad']);
  31.     $peso = intval($_POST['peso']);
  32.     $genero = $_POST['genero'];
  33.     // Validamos con los límites propuestos
  34.     if($edad < 14 || $edad > 100) {
  35.         $errores['edad'] = 'Selecciona tu edad';
  36.     }
  37.     if($peso < 30 || $peso > 200) {
  38.         $errores['peso'] = 'Selecciona tu peso';
  39.     }
  40.     if(!in_array($genero, array('masculino', 'femenino'))) {
  41.         $errores['genero'] = 'Selecciona tu género';
  42.     }
  43.     if(count($errores) = 0) {
  44.         // OK, todo bien, aquí terminas de procesar
  45.     }
  46.     // Si hubo errores se mostrará nuevamente el formulario
  47. }
  48. ?>
  49.         <form method="post" action="index.php" enctype="x-www-form-urlencoded">
  50.             <h3><terror>Sobre tu físico</terror></h3><br/>
  51.             <table border="0" align="left">
  52.                 <tr>
  53.                 <td><enun>Edad</enun></td>
  54.                 <td>
  55. <?php
  56. // Crear select con opción predefinida
  57. html_select('edad', 'edad', $edad, array(), 14, 100);
  58. if(isset($errores['edad'])) {
  59.     echo "<span class=\"error\">{$errores['edad']}</span>";
  60. }
  61. ?>
  62.                 </td>
  63.                 </tr>
  64.                 <tr>
  65.                     <td><enun>Peso</enun></td>
  66.                     <td>
  67. <?php
  68. // Crear select con opción predefinida
  69. html_select('peso', 'peso', $peso, array(), 30, 200);
  70. if(isset($errores['peso'])) {
  71.     echo "<span class=\"error\">{$errores['peso']}</span>";
  72. }
  73. ?>
  74.                     </td>
  75.                 </tr>
  76.                 <tr>
  77.                     <td><enun>Género</enun></td>
  78.                     <td>
  79. <?php
  80. // Crear select con opción predefinida
  81. html_select('genero', 'genero', $genero, array("" => 'Selecciona', 'masculino' => 'Masculino', 'femenino' => 'Femenino'));
  82. if(isset($errores['genero'])) {
  83.     echo "<span class=\"error\">{$errores['genero']}</span>";
  84. }
  85. ?>
  86.                     </td>
  87.                 </tr>
  88.             </table>
  89.             <input type="submit" value="Enviar" class="button" />
  90.         </form>
__________________
- León, Guanajuato
- GV-Foto