Ver Mensaje Individual
  #6 (permalink)  
Antiguo 15/10/2008, 05:59
Keysher
 
Fecha de Ingreso: noviembre-2002
Mensajes: 1.341
Antigüedad: 21 años, 5 meses
Puntos: 17
Respuesta: validar form y si no se cumplen las validaciones volver al form

A lo que yo me refería es a algo de éste estilo:


Código php:
Ver original
  1. <?
  2. function showForm($data, $type) {
  3.  
  4.     $class[0] = 'no_ok';
  5.     $class[1] = 'ok';
  6. ?>
  7.         <form name="form" action="" method="POST">
  8.             <input type="text" name="nombre" class="<?=$class[$type['nombre']]?>" value="<?=$data['nombre'];?>"></input>
  9.             <input type="text" name="apellidos" class="<?=$class[$type['apellidos']]?>" value="<?=$data['apellidos'];?>"></input>
  10.             <input type="submit" name="enviar" value="Enviar"> 
  11.         </form>  
  12. <?
  13.  
  14. }
  15.  
  16.  
  17.  
  18.  
  19. function validate($data, &$type) {
  20.    
  21.     $result = 1;
  22.  
  23.     $nombre = trim($data['nombre']);
  24.     $apellidos = trim($data['apellidos']);
  25.     if (empty($nombre)) {
  26.         $result = 0;
  27.         $type['nombre'] = 0;
  28.     } else
  29.         $type['nombre'] = 1;
  30.     if (empty($apellidos)) {
  31.         $result = 0;
  32.         $type['apellidos'] = 0;
  33.     } else
  34.         $type['apellidos'] = 1;
  35.  
  36.     return $result;
  37.  
  38. }
  39.  
  40.  
  41. ?>
  42. <html>
  43. <head>
  44.     <style type="text/css">
  45.         .ok {
  46.             background: green;
  47.         }
  48.         .no_ok {
  49.             background: red;
  50.         }
  51.  
  52.     </style>
  53.  
  54. </head>
  55.  
  56. <body>
  57.  
  58. <?
  59.  
  60. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  61.  
  62.     $data = $_POST;
  63.     $result = validate($data, $type);
  64.     if ($result == 1)
  65.         echo "TODO OK, aquí redireccionamos, o mostramos lo que sea...";
  66.     else
  67.         showForm($data, $type);
  68. }
  69. else {
  70.     $data['nombre'] = "";
  71.     $data['apellidos'] = "";
  72.     $type['nombre'] = 1;
  73.     $type['apellidos'] = 1;
  74.  
  75.     showForm($data, $type);
  76.  
  77. }
  78.  
  79. ?>
  80.  
  81. </body>
  82. </html>


Es un ejemplo muy básico y optimizable, pero por ahí van los tiros.