La duda que tengo es que al final del codigo esta el :
echo "Registro correcto";
Que se escribe en la misma web, pero lo que yo quiero es redirigir a otra web con un :
header('Location: paso2.php');
Si lo cambio tal cual por el echo me manda el siguiente error:
Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\game\registro\form.php:88) in C:\AppServ\www\game\registro\form.php on line 129
Código PHP:
<?php
include '..\conexion\acceso_db.php';
function validateUsername($name){
//NO cumple longitud minima
if(strlen($name) < 4)
return false;
//SI longitud pero NO solo caracteres A-z
else if(!preg_match("/^[a-zA-Z]+$/", $name))
return false;
// SI longitud, SI caracteres A-z
else
return true;
}
function validatePassword1($password1){
//NO tiene minimo de 5 caracteres o mas de 12 caracteres
if(strlen($password1) < 5 || strlen($password1) > 12)
return false;
// SI longitud, NO VALIDO numeros y letras
else if(!preg_match("/^[0-9a-zA-Z]+$/", $password1))
return false;
// SI rellenado, SI email valido
else
return true;
}
function validatePassword2($password1, $password2){
//NO coinciden
if($password1 != $password2)
return false;
else
return true;
}
function validateEmail($email){
//NO hay nada escrito
if(strlen($email) == 0)
return false;
// SI escrito, NO VALIDO email
else if(!filter_var($_POST['email'], FILTER_SANITIZE_EMAIL))
return false;
// SI rellenado, SI email valido
else
return true;
}
//Comprobacion de datos
//variables valores por defecto
$username = "";
$usernameValue = "";
$password1 = "";
$password2 = "";
$email = "";
$emailValue = "";
//Validacion de datos enviados
if(isset($_POST['send'])){
if(!validateUsername($_POST['username']))
$username = "error";
if(!validatePassword1($_POST['password1']))
$password1 = "error";
if(!validatePassword2($_POST['password1'], $_POST['password2']))
$password2 = "error";
if(!validateEmail($_POST['email']))
$email = "error";
//Guardamos valores para que no tenga que reescribirlos
$usernameValue = $_POST['username'];
$emailValue = $_POST['email'];
//Comprobamos si todo ha ido bien
if($username != "error" && $password1 != "error" && $password2 != "error" && $email != "error")
$status = 1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="es-ES">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Cómo validar un formulario utilizando PHP y Javascript (jQuery) | Web.Ontuts</title>
<link rel="stylesheet" href="main.css" type="text/css" media="screen" />
</head>
<body>
<table width="100%" border="1">
<tr>
<td height="50" colspan="2"><img src="../imagenes/logo.jpg" width="229" height="50" /></td>
</tr>
<tr>
<?php if(!isset($status)): ?>
<td>d fd</td>
<td><form id="form1" action="form.php" method="post">
<label for="username">Usuario</label>
<input tabindex="1" name="username" id="username" type="text" class="text <?php echo $username ?>" value="<?php echo $usernameValue ?>" />
<span id="req-username" class="requisites <?php echo $username ?>">Caracteres A-Z, min 4.</span>
<label for="password1">Contraseña</label>
<input tabindex="2" name="password1" id="password1" type="password" class="text <?php echo $password1 ?>" value="" />
<span id="req-password1" class="requisites <?php echo $password1 ?>">Min 5 caracteres (A-Z,0-9</span>).
<label for="password2">Repetir Contraseña </label>
<input tabindex="3" name="password2" id="password2" type="password" class="text <?php echo $password2 ?>" value="" />
<span id="req-password2" class="requisites <?php echo $password2 ?>">Repetir correctamente.</span>
<label for="email">E-mail </label>
<input tabindex="4" name="email" id="email" type="text" class="text <?php echo $email ?>" value="<?php echo $emailValue ?>" />
<span id="req-email" class="requisites <?php echo $email ?>">Un e-mail válido por favor</span>
<div>
<input tabindex="6" name="send" id="send" type="submit" class="submit" value="Enviar formulario" />
</div>
</form></td>
</tr>
</table>
<?php else:
include '..\conexion\acceso_db.php';
$password = md5($_POST[password1]);
$dia= date ("Y:n:d");
mysql_query( "INSERT usuario (nick,email,pass,fecharegistro) VALUES ('$_POST[username]','$_POST[email]','$password','$dia')");
echo "Registro correcto";
endif;
?>
</div>
</div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
Muchas gracias!