Página del php que valida el formulario:
Código PHP:
<?php
$nombre = $_GET['nombre'];
$telefono=$_GET['telf'];
$email=$_GET['email'];
$nombreC=false;
$telefonoC=false;
$emailC=false;
$datosRecibidos=false;
$datosCorrectos=false;
if($nombre=="" || $telefono=="" || $email==""){
$datosRecibidos=false;
echo '<p>Faltan datos por rellenar</p><br>';
}else{
//Nombre
if(preg_match('[^A-Za-z]', $nombre)){
$nombreC=true;
}else{
$nombreC=false;
echo '<p>El nombre solo puede contener letras</p><br>';
}
//Telefono
if(is_int($telefono)){
if(preg_match('[^0-9]{9}',$telefono)){
$telefonoC=true;
}else{
echo 'El telefono debe contener 9 digitos.';
}
}else{
echo '<p>El telefono solo puede contener numeros.</p> <br>';
}
//Email
if(preg_match('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@+([_a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]{2,200}\.[a-zA-Z]{2,6}$^', $email)){
$emailC=true;
}else{
echo '<p>Email incorrecto.</p><br>';
}
if($nombreC && $telefonoC && $emailC){
echo '<P>DATOS ENVIADOS CORRECTAMENTE</p> <br>' ;
}
}
?>
Página del formulario:
Código HTML:
<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Documento sin título</title> </head> <body> <form action="validar_formulario.php" method="get"> <table width="600" border="1"> <tr> <td>Nombre:</td> <td><input name="nombre" type="text" /></td> </tr> <tr> <td>Telefono:</td> <td><input name="telf" type="text" maxlength="9" /></td> </tr> <tr> <td>Email:</td> <td><input name="email" type="text" size="80" /></td> </tr> <tr> <td><input name="enviar" type="submit" value="ENVIAR"/></td> <td><input name="borrar" type="reset" value="BORRAR" /></td> </tr> </table> </form> </body> </html>
Las dudas son las siguientes:
1ª-> No se como hacer para que en el teléfono haya que introducir obligatoriamente 9 dígitos.
2ª-> Debo tener los if's mal hechos porque no me responde como debería. Si alguien le puede echar un ojo lo agradecería.
3ª-> ¿Es posible que, cuando encuentre un error en el php, te rediriga a la pagina html en el punto donde se ha producido el error? Por ejemplo, si en el nombre mete un número, que te diga el error y te mande al campo de texto del nombre.
De momento creo que ya esta. Espero que me podáis ayudar. Muchas gracias a todos por adelantado.