![Antiguo](http://static.forosdelweb.com/fdwtheme/images/statusicon/post_old.gif)
12/09/2011, 03:32
|
| | Fecha de Ingreso: septiembre-2011 Ubicación: En mi casa
Mensajes: 7
Antigüedad: 13 años, 5 meses Puntos: 1 | |
Respuesta: mi primer formulario php opiniones y como validar campos Cita:
Iniciado por publicityextrim Respondiendo como validar campos, estos los puedes realizar usando JavaScript o directamente en PHP.
Ejemplo con JavaScript
Código HTML:
Ver original<? if ($_POST) { //cuerpo del formulario $cuerpo = "Formulario enviado desde la web:\n"; $cuerpo .= "Nombre: {$_POST['nombre']}\n"; $cuerpo .= "Email: {$_POST['email']}\n"; $cuerpo .= "Telefono: {$_POST['telefono']}"; // nombre de quien envia $cabeza = "From: {$_POST['nombre']} <{$_POST['email']}>"; //envio correo... /* <![CDATA[ */ (function(){try{var s,a,i,j,r,c,l=document.getElementById("__cf_email__");a=l.className;if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})(); /* ]]> */ ", "Formulario recibido", $cuerpo, $cabeza)) { //Agradecimiento, echo " <p style='text-align: center;'><strong>Gracias por tu solicitud. En breve recibiras nuestras noticias. </strong><br /><br /><img src=images/ok.png></p>"; //Paramos todo lo que hay apartir de aquí. exit; } } ?> <!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"> <meta http-equiv="Content-Type" content="text/html; charset=latin1;"> <script type="text/javascript"> function validaform(){ var nombre=document.getElementById('nombre').value; var email=document.getElementById('email').value; var telefono=document.getElementById('telefono').value; if(nombre==""||nombre.length<5){ alert('El campo nombre es invalido'); document.getElementById('nombre').focus(); } if(email==""){ alert('El campo email es invalido'); document.getElementById('email').focus(); }else{ if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){ alert('El campo email es invalido'); document.getElementById('email').focus(); } } if(telefono==""){ alert('El campo telefono es invalido'); document.getElementById('telefono').focus(); }else{ if (!/^([0-9])*$/.test(telefono)){ alert('El campo telefono es invalido'); document.getElementById('telefono').focus(); } } document.getElementById('form').submit(); } <form id="form" method="POST"> <input type="text" name="nombre" id="nombre"> <input type="text" name="email" id="email"> <input type="text" name="telefono" id="telefono"> <input type="image" src="images/button.png" onclick="validaform()" alt="Solicitar más información ahora">
Al incluir la validación con java que has propuesto, salta un error en la línea 17:
Parse error: syntax error, unexpected T_STRING ... on line 17 |