Ver Mensaje Individual
  #6 (permalink)  
Antiguo 10/09/2011, 10:54
publicityextrim
 
Fecha de Ingreso: agosto-2011
Mensajes: 42
Antigüedad: 12 años, 8 meses
Puntos: 8
Respuesta: mi primer formulario php opiniones y como validar campos

Respondiendo como validar campos, estos los puedes realizar usando JavaScript o directamente en PHP.

Ejemplo con JavaScript

Código HTML:
Ver original
  1. <?
  2.  
  3.    if ($_POST) {
  4.    
  5.        //cuerpo del formulario
  6.        $cuerpo  = "Formulario enviado desde la web:\n";
  7.        $cuerpo .= "Nombre: {$_POST['nombre']}\n";
  8.        $cuerpo .= "Email: {$_POST['email']}\n";
  9.        $cuerpo .= "Telefono: {$_POST['telefono']}";
  10.  
  11.        // nombre de quien envia
  12.        $cabeza = "From: {$_POST['nombre']} <{$_POST['email']}>";
  13.  
  14.         //envio correo...
  15.         if (mail("[email protected]
  16. /* <![CDATA[ */
  17. (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){}})();
  18. /* ]]> */
  19. ", "Formulario recibido", $cuerpo, $cabeza)) {
  20.        
  21.             //Agradecimiento,
  22.             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>";
  23.        
  24.             //Paramos todo lo que hay apartir de aquí.
  25.             exit;
  26.            
  27.         }
  28.        
  29.     }
  30.  
  31. ?>
  32.  
  33. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  34. <html xmlns="http://www.w3.org/1999/xhtml">
  35.  
  36.     <head>
  37.    
  38.         <meta http-equiv="Content-Type" content="text/html; charset=latin1;">
  39.        
  40.         <script type="text/javascript">
  41.             function validaform(){
  42.                 var nombre=document.getElementById('nombre').value;
  43.                 var email=document.getElementById('email').value;
  44.                 var telefono=document.getElementById('telefono').value;
  45.                
  46.                 if(nombre==""||nombre.length<5){
  47.                     alert('El campo nombre es invalido');
  48.                     document.getElementById('nombre').focus();
  49.                 }
  50.                 if(email==""){
  51.                     alert('El campo email es invalido');
  52.                     document.getElementById('email').focus();
  53.                 }else{
  54.                     if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
  55.                         alert('El campo email es invalido');
  56.                         document.getElementById('email').focus();
  57.                     }
  58.                 }
  59.                 if(telefono==""){
  60.                     alert('El campo telefono es invalido');
  61.                     document.getElementById('telefono').focus();
  62.                 }else{
  63.                     if (!/^([0-9])*$/.test(telefono)){
  64.                         alert('El campo telefono es invalido');
  65.                         document.getElementById('telefono').focus();
  66.                     }
  67.                 }
  68.                 document.getElementById('form').submit();
  69.             }
  70.         </script>
  71.    
  72.     </head>
  73.    
  74.     <body>
  75.    
  76.         <form id="form" method="POST">
  77.        
  78.             <label for="nombre"><strong>Nombre:</strong></label><br />
  79.             <input type="text" name="nombre" id="nombre">
  80.            
  81.             <br /><br />
  82.            
  83.             <label for="email"><strong>Email:</strong></label><br />
  84.             <input type="text" name="email" id="email">
  85.            
  86.             <br /><br />
  87.            
  88.             <label for="telefono"><strong>Teléfono:</strong></label><br />
  89.             <input type="text" name="telefono" id="telefono">
  90.            
  91.             <br /><br />
  92.  
  93.             <input type="image" src="images/button.png" onclick="validaform()" alt="Solicitar más información ahora">
  94.            
  95.         </form>    
  96.    
  97.     </body>
  98.    
  99. </html>
__________________
Publicityextrim - http://www.publicityextrim.com
Email: [email protected]
..Lo que tu empresa necesita..