Ver Mensaje Individual
  #7 (permalink)  
Antiguo 01/03/2009, 22:05
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Duda de cómo validar un mismo formulario para varias acciones

Pues algo simple:
Código php:
Ver original
  1. <?php
  2. if( isset( $_POST['submit'] ) ) {
  3.        $noErrors = true;
  4.        $errors = array();
  5.        if( empty( $_POST['nombre'] ) ) {
  6.               $noErrors = false;
  7.               $errors['nombre'] = "Debe de escribir un nombre";
  8.        }
  9.        if( !is_numeric( $_POST['edad'] ) ) {
  10.               $noErrors = false;
  11.               $errors['edad'] = "Debe de escribir su edad en numeros";
  12.        }
  13.  
  14.        if( $noErrors ) {
  15.              // aqui haces todo tu proceso de insercion, registro, etc.
  16.              header("Location: exito.php");
  17.        }
  18. }
  19. ?>
  20. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  21. <input type="text" name="nombre" value="<?php echo $_POST['nombre']; ?>" /> <?php if( !empty( $errors['nombre'] )) { echo $errors['nombre']; } ?>
  22. <input type="text" name="edad" value="<?php echo $_POST['edad']; ?>" /> <?php if( !empty( $errors['edad'] )) { echo $errors['edad']; } ?>
  23. <input type="submit" name="submit" value="Registrarse" />
  24. </form>

Saludos