Foros del Web » Programando para Internet » PHP »

formulario a varios correos con checkbox

Estas en el tema de formulario a varios correos con checkbox en el foro de PHP en Foros del Web. buenas noches amigos, tengo una duda la cual requiero ayuda, resulta que en mi web estoy montando un formulario para correo electronico, el detalle es ...
  #1 (permalink)  
Antiguo 10/10/2012, 21:32
 
Fecha de Ingreso: abril-2009
Mensajes: 5
Antigüedad: 15 años
Puntos: 0
Información formulario a varios correos con checkbox

buenas noches amigos, tengo una duda la cual requiero ayuda, resulta que en mi web estoy montando un formulario para correo electronico, el detalle es que estoy colocando unos checkbox los cuales cada uno indica un correo diferente al cual seran enviados los datos, lo que quiero es que solo se envie el correo a los checkbox que estan marcados, les mando un ejemplo de mi codigo...

contactalo.php

<!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>CONTACTENOS</title>

</head>
<body>
<form id="contacto" name="contacto" method="post" action="kp-prueba.php">
<center><table width="100%" height="100%" border="0">
<tr>
<td width="888" height="248"><CENTER>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table width="500" height="222" border="0" align="center" cellpadding="0" cellspacing="0" style="background-color:transparent">
<tr>
<td colspan="3" align="left"><strong class="Estilo9"><em><font style="font-size:20px">Consultas e Inquietudes:</font></em></strong></td>
</tr>

<tr>
<td width="18" height="32">&nbsp;</td>
<td width="119"><em><font style="font-size:16px">Nombre:</font></em><br>
&nbsp;</td>
<td width="363" align="left"><label><input type="text" name="nombre" id="nombre" maxlength="30"/></label><br>
&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><em><font style="font-size:16px">Apellido:</font></em><br>
&nbsp;</td>
<td align="left"><label><input type="text" name="apellido" id="apellido" maxlength="30"/></label><br>
&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><em><font style="font-size:16px">Teléfono:</font></em><br>
&nbsp;</td>
<td align="left"><label><input type="text" name="tlf" id="tlf" maxlength="15"/></label><br>
&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><em><font style="font-size:16px">E-mail:</font></em><br>
&nbsp;</td>
<td align="left"><label><input type="text" name="mail" id="mail"/></label><br>
&nbsp;</td>
</tr>
</table>
<input type="checkbox" name="cor1" /> correo1<br>
<input type="checkbox" name="cor2" /> correo2<br>
<input type="checkbox" name="cor3" /> correo3<br>
<p>&nbsp;</p>
<table width="500" height="159" border="0" align="center" cellpadding="0" cellspacing="0" style="background-color:transparent">
<tr>
<td align="left"><strong class="Estilo9"><em><font style="font-size:20px">Déjenos un Mensaje:</font></em></strong></td>
</tr>
<tr>
<td align="center"><p>
<label><textarea name="mensaje" id="mensaje"></textarea></label>
</p></td>
</tr>
<tr>
<td align="center"><input type="submit" name="OK" value="Enviar" onclick="alert('Su mensaje fue enviado Satisfactoriamente...')"/></td>
</tr>
</table>
<p>&nbsp;</p>
</CENTER>
</td>
</tr>
</table></center>
</form>
</body>
</html>


----------------------------------------------------------------------------------------------------


y aqui esta el que es llamado...

kp-prueba.php

<?php

$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$tlf = $_POST['tlf'];
$mail = $_POST['mail'];
$cor1 = $_POST['correo1'];
$cor2 = $_POST['correo2'];
$cor3 = $_POST['correo3'];

$header = 'From: ' . $mail . " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";

$mensaje .= "Este mensaje fue enviado por: \r\n\n";
$mensaje .= "Nombre: " . $nombre . " \r\n";
$mensaje .= "Apellido: " . $apellido . " \r\n";
$mensaje .= "Telefono: " . $tlf . " \r\n";
$mensaje .= "Su e-mail es: " . $mail . " \r\n";
$mensaje .= "Mensaje: " . $_POST['mensaje'] . " \r\n";
$mensaje .= "Enviado el: " . date('d/m/Y', time());

if ($OK == "Enviar") {

if ( $cor1 == "on" ){ $para = '[email protected]'; $asunto = 'Consulta'; };

if ( $cor2 == "on" ){ $para = '[email protected]'; $asunto = 'Consulta'; };

if ( $cor3 == "on" ){ $para = '[email protected]'; $asunto = 'Consulta'; };

};


mail($para, $asunto, utf8_decode($mensaje), $header);
echo "<script>document.location.href='contactalo.ph p';</script>\n";

?>
  #2 (permalink)  
Antiguo 10/10/2012, 21:46
Avatar de rodrigo791  
Fecha de Ingreso: noviembre-2009
Ubicación: Uruguay
Mensajes: 1.339
Antigüedad: 14 años, 5 meses
Puntos: 168
Respuesta: formulario a varios correos con checkbox

bienvenido,
1- volve a poner todo el codigo que pusiste, por separado, usando el combo que tenes el el foro también llamado Highlight, 1 highlight para el primero código usando html, y uno usando php para el segundo.
2 - cual es el problema del asunto?

entienda que es mejor para todos tener el codigo dentro del highlight, para tenerlo mas ordenado, que se lea mejor y que nuestros ojos no sangren ::

Código HTML:
Ver original
  1. <h1>hola!!!</h1>

así
  #3 (permalink)  
Antiguo 12/10/2012, 12:41
 
Fecha de Ingreso: abril-2009
Mensajes: 5
Antigüedad: 15 años
Puntos: 0
Respuesta: formulario a varios correos con checkbox

gracias amigo, los 2 son php, aqui estoy montando los 2 codigos...

contactalo.php

Código PHP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>CONTACTENOS</title>
  6.  
  7. </head>
  8. <body>
  9. <form id="contacto" name="contacto" method="post" action="kp-prueba.php">
  10. <center><table width="100%" height="100%" border="0">
  11. <tr>
  12. <td width="888" height="248"><CENTER>
  13. <p>&nbsp;</p>
  14. <p>&nbsp;</p>
  15. <table width="500" height="222" border="0" align="center" cellpadding="0" cellspacing="0" style="background-color:transparent">
  16. <tr>
  17. <td colspan="3" align="left"><strong class="Estilo9"><em><font style="font-size:20px">Consultas e Inquietudes:</font></em></strong></td>
  18. </tr>
  19.  
  20. <tr>
  21. <td width="18" height="32">&nbsp;</td>
  22. <td width="119"><em><font style="font-size:16px">Nombre:</font></em><br>
  23. &nbsp;</td>
  24. <td width="363" align="left"><label><input type="text" name="nombre" id="nombre" maxlength="30"/></label><br>
  25. &nbsp;</td>
  26. </tr>
  27. <tr>
  28. <td>&nbsp;</td>
  29. <td><em><font style="font-size:16px">Apellido:</font></em><br>
  30. &nbsp;</td>
  31. <td align="left"><label><input type="text" name="apellido" id="apellido" maxlength="30"/></label><br>
  32. &nbsp;</td>
  33. </tr>
  34. <tr>
  35. <td>&nbsp;</td>
  36. <td><em><font style="font-size:16px">Teléfono:</font></em><br>
  37. &nbsp;</td>
  38. <td align="left"><label><input type="text" name="tlf" id="tlf" maxlength="15"/></label><br>
  39. &nbsp;</td>
  40. </tr>
  41. <tr>
  42. <td>&nbsp;</td>
  43. <td><em><font style="font-size:16px">E-mail:</font></em><br>
  44. &nbsp;</td>
  45. <td align="left"><label><input type="text" name="mail" id="mail"/></label><br>
  46. &nbsp;</td>
  47. </tr>
  48. </table>
  49. <input type="checkbox" name="cor1" /> correo1<br>
  50. <input type="checkbox" name="cor2" /> correo2<br>
  51. <input type="checkbox" name="cor3" /> correo3<br>
  52. <p>&nbsp;</p>
  53. <table width="500" height="159" border="0" align="center" cellpadding="0" cellspacing="0" style="background-color:transparent">
  54. <tr>
  55. <td align="left"><strong class="Estilo9"><em><font style="font-size:20px">Déjenos un Mensaje:</font></em></strong></td>
  56. </tr>
  57. <tr>
  58. <td align="center"><p>
  59. <label><textarea name="mensaje" id="mensaje"></textarea></label>
  60. </p></td>
  61. </tr>
  62. <tr>
  63. <td align="center"><input type="submit" name="OK" value="Enviar" onclick="alert('Su mensaje fue enviado Satisfactoriamente...')"/></td>
  64. </tr>
  65. </table>
  66. <p>&nbsp;</p>
  67. </CENTER>
  68. </td>
  69. </tr>
  70. </table></center>
  71. </form>
  72. </body>
  73. </html>


kp-prueba.php

Código PHP:
Ver original
  1. <?php
  2.  
  3. $nombre = $_POST['nombre'];
  4. $apellido = $_POST['apellido'];
  5. $tlf = $_POST['tlf'];
  6. $mail = $_POST['mail'];
  7. $cor1 = $_POST['correo1'];
  8. $cor2 = $_POST['correo2'];
  9. $cor3 = $_POST['correo3'];
  10.  
  11. $header = 'From: ' . $mail . " \r\n";
  12. $header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
  13. $header .= "Mime-Version: 1.0 \r\n";
  14. $header .= "Content-Type: text/plain";
  15.  
  16. $mensaje .= "Este mensaje fue enviado por: \r\n\n";
  17. $mensaje .= "Nombre: " . $nombre . " \r\n";
  18. $mensaje .= "Apellido: " . $apellido . " \r\n";
  19. $mensaje .= "Telefono: " . $tlf . " \r\n";
  20. $mensaje .= "Su e-mail es: " . $mail . " \r\n";
  21. $mensaje .= "Mensaje: " . $_POST['mensaje'] . " \r\n";
  22. $mensaje .= "Enviado el: " . date('d/m/Y', time());
  23.  
  24. if ($OK == "Enviar") {
  25.  
  26. if ( $cor1 == "on" ){ $para = '[email protected]'; $asunto = 'Consulta'; };
  27.  
  28. if ( $cor2 == "on" ){ $para = '[email protected]'; $asunto = 'Consulta'; };
  29.  
  30. if ( $cor3 == "on" ){ $para = '[email protected]'; $asunto = 'Consulta'; };
  31.  
  32. };
  33.  
  34.  
  35. mail($para, $asunto, utf8_decode($mensaje), $header);
  36. echo "<script>document.location.href='contactalo.ph p';</script>\n";
  37.  
  38. ?>
  #4 (permalink)  
Antiguo 12/10/2012, 13:52
Avatar de Javier01  
Fecha de Ingreso: febrero-2008
Ubicación: Montevideo
Mensajes: 261
Antigüedad: 16 años, 1 mes
Puntos: 31
Respuesta: formulario a varios correos con checkbox

Buenas tardes,

1ero tus checkbox se llaman cor1 y preguntas por $_POST['correo1'];
2do la variable $mensaje deberías declararla antes de empezar a usarla. $mensaje = '';
3ero Preguntas si $OK == "Enviar" pero nunca definiste la variable $OK
4to Siempre la variable $para va tener un correo solo, puesto que en cada if que entre se vuelve a cargar, pero no se une al contenido anterior.
si quieres que se una deberías declárala antes de los if como vacía $para =''; y en cada if seria $para .=

Saludos
__________________
Tomarse un tiempo para redactar correctamente la pregunta, utilizando los signos de puntuación adecuados, es ganar tiempo y calidad en la respuesta.
  #5 (permalink)  
Antiguo 12/10/2012, 16:21
Avatar de afrika026  
Fecha de Ingreso: junio-2011
Mensajes: 85
Antigüedad: 12 años, 9 meses
Puntos: 19
Respuesta: formulario a varios correos con checkbox

el error esta en la verificacion del estado de los checkbox.

Primero: borra las lineas
$cor1 = $_POST['correo1'];
$cor2 = $_POST['correo2'];
$cor3 = $_POST['correo3'];

Segundo: declaras $para como vacía, porque ahi se van a ir agregando los mails seleccionados.
entonces:

$para="";

Tercero: La verificacion de los checkboxs se hace con isset($_POST['correo1'])


El codigo sería:

if (isset($_POST['correo1'])){ $para. = '[email protected]'; $asunto = 'Consulta'; };

if (isset($_POST['correo2'])){ $para. = '[email protected]'; $asunto = 'Consulta'; };

if (isset($_POST['correo3'])){ $para. = '[email protected]'; $asunto = 'Consulta'; };

Última edición por afrika026; 12/10/2012 a las 16:31
  #6 (permalink)  
Antiguo 12/10/2012, 23:09
 
Fecha de Ingreso: abril-2009
Mensajes: 5
Antigüedad: 15 años
Puntos: 0
Respuesta: formulario a varios correos con checkbox

gracias amigos, sin embargo si no seria mucho pedir pueden tomar mi codigo y plantearmelo como deberia ir? gracias
  #7 (permalink)  
Antiguo 16/10/2012, 19:28
 
Fecha de Ingreso: abril-2009
Mensajes: 5
Antigüedad: 15 años
Puntos: 0
Respuesta: formulario a varios correos con checkbox

Cita:
Iniciado por afrika026 Ver Mensaje
el error esta en la verificacion del estado de los checkbox.

Primero: borra las lineas
$cor1 = $_POST['correo1'];
$cor2 = $_POST['correo2'];
$cor3 = $_POST['correo3'];

Segundo: declaras $para como vacía, porque ahi se van a ir agregando los mails seleccionados.
entonces:

$para="";

Tercero: La verificacion de los checkboxs se hace con isset($_POST['correo1'])


El codigo sería:

if (isset($_POST['correo1'])){ $para. = '[email protected]'; $asunto = 'Consulta'; };

if (isset($_POST['correo2'])){ $para. = '[email protected]'; $asunto = 'Consulta'; };

if (isset($_POST['correo3'])){ $para. = '[email protected]'; $asunto = 'Consulta'; };
gracias amigos, sin embargo si no seria mucho pedir pueden tomar mi codigo y plantearmelo como deberia ir? gracias
  #8 (permalink)  
Antiguo 17/10/2012, 12:33
Avatar de Javier01  
Fecha de Ingreso: febrero-2008
Ubicación: Montevideo
Mensajes: 261
Antigüedad: 16 años, 1 mes
Puntos: 31
Respuesta: formulario a varios correos con checkbox

cambiando el codigo PHP por el que te paso, creo que te funcionará.

Pero de todas formas hay muchas cosas para mejorar, como:
-revisar con isset todos los campos, como nombre, apellido, etc.
-arreglar para que llamando directamente la pagina php no de error
-redireccion con header de php en vez de con javascript
-otras.

Código PHP:
<?php
$nombre 
$_POST['nombre'];
$apellido $_POST['apellido'];
$tlf $_POST['tlf'];
$mail $_POST['mail'];
 
$header 'From: ' $mail " \r\n";
$header .= "X-Mailer: PHP/" phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";
 
$mensaje '';
$mensaje .= "Este mensaje fue enviado por: \r\n\n";
$mensaje .= "Nombre: " $nombre " \r\n";
$mensaje .= "Apellido: " $apellido " \r\n";
$mensaje .= "Telefono: " $tlf " \r\n";
$mensaje .= "Su e-mail es: " $mail " \r\n";
$mensaje .= "Mensaje: " $_POST['mensaje'] . " \r\n";
$mensaje .= "Enviado el: " date('d/m/Y'time());
 
if (isset(
$_POST["OK"]) && $_POST["OK"] == "Enviar") {
    
$asunto='Consulta';
    
$para='';
    if(isset(
$_POST["cor1"])){$para .='[email protected];';}
    if(isset(
$_POST["cor2"])){$para .='[email protected];';}
    if(isset(
$_POST["cor3"])){$para .='[email protected];';}
}
if(
$para!='' && $asunto!=''){ 
    
mail($para$asuntoutf8_decode($mensaje), $header);
}
echo 
"<script>document.location.href='contactalo.php';</script>\n";
 
?>
saludos
__________________
Tomarse un tiempo para redactar correctamente la pregunta, utilizando los signos de puntuación adecuados, es ganar tiempo y calidad en la respuesta.

Etiquetas: checkbox, correos, formulario, html
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:21.