Ver Mensaje Individual
  #9 (permalink)  
Antiguo 09/07/2012, 12:24
Avatar de patilanz
patilanz
 
Fecha de Ingreso: enero-2012
Mensajes: 880
Antigüedad: 12 años, 3 meses
Puntos: 29
Respuesta: Problema al validar unos campos

Hola perdonen me por haber puesto de que me funciono...
La verdead esq me funciono pero al anadir unas cositas me imprimi simplemente un 0 nada mas y antes todo funcionaba!!!
El codigo es este:

Código PHP:
Ver original
  1. <?php
  2. //Primero vamos a crear una funcion para los errores
  3. function errores() {
  4.     $errores = array();
  5. $value = $_POST;
  6.     //Comprobamos el nombre:
  7.     if(strlen($value['nombre']) < 2) {
  8.         $errores[0] = '<font color="#FF0000">Por favor escribe un nombre correcto!</font>';
  9.     }
  10.     if(strlen($value['nombre']) > 10) {
  11.         $errores[1] = '<font color="#FF0000">Por favor escriba un nombre correcto!</font>';
  12.     }
  13.     //Comprobamos el email con un patron solo para emails
  14.     if(!preg_match("/^[^\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/",$value['email'])) {
  15.         $errores[2] = '<font color="#FF0000">Por favor escriba un email valido!</font>';   
  16.     }
  17.     //Comprobamos el lugar por si alguien escribe algo con 2 caracteres (Si no escribe nada no se comprobara)
  18.     if(isset($value['lugar']) && !empty($value['lugar'])) {
  19.         if(strlen($value['lugar']) < 2) {
  20.             $errores[3] = '<font color="#FF0000">Por favor escriba un lugar valido!</font>';
  21.         }
  22.         //Aqui es comprueba si es mas de 30 caracteres para evitar spam
  23.         if(strlen($value['lugar']) > 30) {
  24.             $errores[4] = '<font color="#FF0000">Por favor escriba un lugar valido!</font>';   
  25.         }  
  26.     } //Terminamos la comprobacion del lugar
  27.     //Validamos los comentarios
  28.     if(strlen($value['comentario']) < 10) {
  29.         $errores[5] = '<font color="#FF0000">Por favor escriba un comentario mas largo!</font>';
  30.     }
  31.     if(strlen($value['comentario']) > 1000) {
  32.         $errores[6] = '<font color="#FF0000">Por favor escriba un comentario mas largo!</font>'; //En este caso seria spam!
  33.     }
  34.     return $errores; //Aqui cogemos el array errores para usarlo posteriormente
  35. } //Terminamos la funcion
  36. $value = $_POST;
  37. if($value['oculto']) { //Comprobamos si el campo oculto existe (en caso falso se printea en la pantalla y se vuelve a comprobar.En caso de que si existe se continua con la comprobacion)
  38.     if(!empty($value['nombre']) && isset($value['nombre']) &&
  39.         !empty($value['email']) && isset($value['email']) &&
  40.         !empty($value['comentario']) && isset($value['comentario'])) { //En este if se comprueba si los campos nombre, email y comentario no estan vacios En caso de que si se printea un error.
  41.         $errores = errores(); //Metemos la funcion dentro de la variable $errores para usarla mejor
  42.         if(empty($errores)) { //Si los errores no existen... creara y enviara el mensaje
  43.         //Ahora vamos a crear los datos necesarios para el mensaje---
  44.             $fecha=date("d-m-Y");
  45.             $hora=date("H:i:s");
  46.             $destino = 'Tu email';//En esta parte escribiis el email al que quereis que llegue el mensaje
  47.             $asunto = 'Comentario';
  48.             //htmlentities sirve para convertir los caracteres html en caracteres codificados de manera que se escriban directamente y el navegador no los interprete
  49.             $desde = "From: ".htmlentities($value['email']);
  50.             //Creamos el mensaje
  51.             if(!empty($value['lugar']) && isset($value['lugar'])) { //Si el lugar esta escrito lo pondremos en el mensaje. En caso contrario no estara.
  52.                 $mensaje = '
  53.             Nombre: ' + htmlentities($value['nombre']) + '\n
  54.             Email: ' + htmlentities($value['email']) + '\n
  55.             Lugar: ' + htmlentities($value['lugar']) + '\n
  56.             Comentario: ' + $value['comentario'] + '\n
  57.             Enviado: ' + $fecha + 'a las ' + $hora + '\n \n';  
  58.             } else {
  59.                 $mensaje = '
  60.             Nombre: ' + htmlentities($value['nombre']) + '\n
  61.             Email: ' + htmlentities($value['email']) + '\n
  62.             No se escribio lugar. \n
  63.             Comentario: ' + htmlentities($value['comentario']) + '\n
  64.             Enviado: ' + $fecha + 'a las ' + $hora + '\n \n';  
  65.             }
  66.             print '<p style="background-color:green; border-radius:20; padding:20; align="center"">';
  67.             mail($destino, $asunto, $mensaje, $desde); //Enviamos el mensaje
  68.             print 'EL mensaje ha sido enviado!';
  69.             print '</p>';
  70.         } else { //Si existen errores... printearlos con la funcion implode
  71.                 print '<form method="post" action="formularito.php">
  72. <table width="200" border="1" bordercolor="#0066FF">
  73.  <tr>
  74.    <td>*Nombre:</td>
  75.    <td><label for="nombre"></label>
  76.    <input type="text" name="nombre" id="nombre" value="' + htmlentities($value['nombre']) + '"></td>
  77.  </tr>
  78.  <tr>
  79.    <td>*Email:</td>
  80.    <td><label for="email"></label>
  81.      <input type="text" name="email" id="email" value="' + htmlentities($value['email']) + '"></td>
  82.  </tr>
  83.  <tr>
  84.    <td>Lugar:</td>
  85.    <td><label for="lugar"></label>
  86.      <input type="text" name="lugar" id="lugar" value="' + htmlentities($value['lugar']) + '"></td>
  87.  </tr>
  88.  <tr>
  89.    <td>*Comentario:</td>
  90.    <td><label for="comentario"></label>
  91.      <textarea name="comentario" id="comentario" cols="21" rows="5">' + htmlentities($value['comentario']) + '</textarea></td>
  92.  </tr>
  93.  </table>
  94. <table width="200" border="0">
  95.  <tr>
  96.    <td><input type="submit" name="button" id="button" value="Enviar" style="border-radius:20px; background-color:#0CC;"></td>
  97.  </tr>
  98. </table>
  99.  
  100. <table width="500" border="0">
  101.  <tr>
  102.    <td><h3>Por favor corrige los siguientes errores: <br /><font color="#FF0000"><ul><li>';
  103.    
  104.                 print implode('</li><li>', errores());
  105.                 print '</li></ul></font>';
  106.                 print'   </td>
  107.  </tr>
  108. </table>
  109.  
  110. <p>&nbsp;</p>
  111. <input type="hidden" value="1" name="oculto" />
  112. </form>';
  113.            
  114.            
  115.         }
  116.     }else { //Si los campos necesarios no estan llenos... Printear un aviso de error con los campos
  117.         print '<form method="post" action="formularito.php">
  118. <table width="200" border="1" bordercolor="#0066FF">
  119.  <tr>
  120.    <td>*Nombre:</td>
  121.    <td><label for="nombre"></label>
  122.    <input type="text" name="nombre" id="nombre" value="' + htmlentities($value['nombre']) + '"></td>
  123.  </tr>
  124.  <tr>
  125.    <td>*Email:</td>
  126.    <td><label for="email"></label>
  127.      <input type="text" name="email" id="email" value="' + htmlentities($value['email']) + '"></td>
  128.  </tr>
  129.  <tr>
  130.    <td>Lugar:</td>
  131.    <td><label for="lugar"></label>
  132.      <input type="text" name="lugar" id="lugar" value="' + htmlentities($value['lugar']) + '"></td>
  133.  </tr>
  134.  <tr>
  135.    <td>*Comentario:</td>
  136.    <td><label for="comentario"></label>
  137.      <textarea name="comentario" id="comentario" cols="21" rows="5">' + htmlentities($value['comentario']) + '</textarea></td>
  138.  </tr>
  139.  </table>
  140. <table width="317" border="0">
  141.  <tr>
  142.    <td><p>
  143.      <input type="submit" name="button" id="button" value="Enviar" style="border-radius:20px; background-color:#0CC;">
  144.    </p>
  145.      <font style="color:#FF0000"><h3>Por favor rellene los campos necesarios!</h3></font></p></td>
  146.  </tr>
  147. </table>
  148. <p>&nbsp;</p>
  149. <input type="hidden" value="1" name="oculto" />
  150. </form>';  
  151.     }  
  152. }else{ //Si el campos oculto no existe printearlo con los demas campos
  153.     print '<form method="post" action="formularito.php">
  154. <table width="200" border="1" bordercolor="#0066FF">
  155.  <tr>
  156.    <td>*Nombre:</td>
  157.    <td><label for="nombre"></label>
  158.    <input type="text" name="nombre" id="nombre" value="' + htmlentities($value['nombre']) + '"></td>
  159.  </tr>
  160.  <tr>
  161.    <td>*Email:</td>
  162.    <td><label for="email"></label>
  163.      <input type="text" name="email" id="email" value="' + htmlentities($value['email']) + '"></td>
  164.  </tr>
  165.  <tr>
  166.    <td>Lugar:</td>
  167.    <td><label for="lugar"></label>
  168.      <input type="text" name="lugar" id="lugar" value="' + htmlentities($value['lugar']) + '"></td>
  169.  </tr>
  170.  <tr>
  171.    <td>*Comentario:</td>
  172.    <td><label for="comentario"></label>
  173.      <textarea name="comentario" id="comentario" cols="21" rows="5">' + htmlentities($value['comentario']) + '</textarea></td>
  174.  </tr>
  175.  </table>
  176. <table width="200" border="0">
  177.  <tr>
  178.    <td><input type="submit" name="button" id="button" value="Enviar" style="border-radius:20px; background-color:#0CC;"></td>
  179.  </tr>
  180. </table>
  181. <p>&nbsp;</p>
  182. <input type="hidden" value="1" name="oculto" />
  183. </form>';  
  184. }
  185. ?>
El problema aparecio cuando anadi entre los campos
Código PHP:
Ver original
  1. htmlentities($value['nombre_del_campos'])
Y no tengo ni idea porque!
Pls ayuden me a resolver lo lo necesito urgente