Form with checkboxes - Do not send the "no checked"

0

I have a form on an html page with several checkboxes, of which (being options) is not necessary to be marked, but in the email I get the blank spaces between "checked" & "checked":

$opcion1 .= $_POST['marcador1'];
$opcion2 .= $_POST['marcador2'];
$opcion3 .= $_POST['marcador3'];
$opcion4 .= $_POST['marcador4'];

When the user dials option 1 and option 4 I receive the email with two blank lines:

Opcion 1
   // Espacio en blanco
   // Espacio en blanco
Opcion 4

What is the code in the php that evaluates the checkboxes and eliminates the unmarked ones, that is: does not send them to the email , in order to remove the jumps of useless line in the body of the message?

    
asked by Mauricio Arias Olave 21.03.2017 в 14:44
source

3 answers

0

$cuerpo_mensaje .= " " . $marcador1 . " \r\n"; there you have a blank space ( " " ) and the line breaks ( \r\n )

Replaces

$cuerpo_mensaje .= " " . $marcador1 . " \r\n";
$cuerpo_mensaje .= " " . $marcador2 . " \r\n";

for

if($marcador1 != ""){
   $cuerpo_mensaje .= " " . $marcador1 . " \r\n";
}
if($marcador2 != ""){
   $cuerpo_mensaje .= " " . $marcador2 . " \r\n";
}
    
answered by 22.03.2017 / 14:16
source
3

You can use the isset() function. This function evaluates if the variable has been defined. If the variable has not been defined, do not use it or use it when you are sending mail. the function returns a boolean.

if(isset( $_POST['marcador1']){
  $opcion1 = $_POST['marcador1'];
}
if(isset( $_POST['marcador2']){
  $opcion2 = $_POST['marcador2'];
}
if(isset( $_POST['marcador3']){
  $opcion3 = $_POST['marcador3'];
}
if(isset( $_POST['marcador4']){
  $opcion4 = $_POST['marcador4'];
}

and just do not take those that are not defined.

    
answered by 21.03.2017 в 14:53
0

Thanks friends.

Well, I have something like that (22 ckeckboxes and none is obligatory), and that's what I was saying: If the visitor dials the 2 option, the 13 and the 22 , in the email I get breaks of intermediate lines (the ckeckboxes unchecked), and it is very ugly as is the body of the message.

<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
echo '<script type="text/javascript">
window.location.assign("url.html");
</script>';
//---------
$opcion1 .= $_POST['marcador1'];
$opcion2 .= $_POST['marcador2'];
$opcion3 .= $_POST['marcador3'];
$opcion4 .= $_POST['marcador4'];
.
.
$opcion22 .= $_POST['marcador22'];
//---------
$nombre .= $_POST['nombre'];
$mail .= $_POST['email'];
$mensaje .= $_POST['mensaje'];
//---------
$header .= 'From: ' . $mail . " \r\n";
$header .= "X-Mailer: PHP/" . phpversion() . " \r\n";
$header .= "Mime-Version: 1.0 \r\n";
$header .= "Content-Type: text/plain";
//---------
$cuerpo_mensaje .= "Enviado por: " . $nombre . " \r\n";
$cuerpo_mensaje .= "E-Mail: " . $mail . " \r\n";
$cuerpo_mensaje .= "Mensaje:" . " \r\n\r\n";
//---------
$cuerpo_mensaje .= " " . $marcador1 . " \r\n";
$cuerpo_mensaje .= " " . $marcador2 . " \r\n";
$cuerpo_mensaje .= " " . $marcador3 . " \r\n";
$cuerpo_mensaje .= " " . $marcador4 . " \r\n";
.
.
$cuerpo_mensaje .= " " . $marcador22 . " \r\n\r\n";
//----------
// Aquí el codigo sigue con otros $cuerpo_mensaje adicionales
//----------
$para = 'aqui_el_correo';
$asunto = 'el_asunto';
mail($para, $asunto, utf8_decode($cuerpo_mensaje), $header, '-faqui_el_correo');
}
?>

Where do I put all the isset ...?

if(isset( $_POST['marcador x']){
  $opcion x = $_POST['marcador x'];
}

Note that I only want to avoid receiving the indicated unchecked:

$cuerpo_mensaje .= " " . $marcador x . " \r\n";

I do not clarify much with this (php is not mine).

Thank you very much!

    
answered by 22.03.2017 в 03:40