Send checkbox values chosen by email

Asked

Viewed 109 times

0

Good afternoon.

I am developing a form but the values chosen in my checkbox do not arrive in the email, since the others arrive. How can I make my form read the checkbox and send the information to my email ?

Follows the code.

<form action="agenda-planejamentorh2.php" method="post">
  <h2>+IFORMAÇOES TREINAMENTO PLANEJAMENTO DE BUDGET DE RH</h2>
  <div class="row cf">
    <p>Seu Nome:</p>
    <input class="text-input" type="text" name="name" id="name"> </div>
  <div class="row cf">
    <p>Seu E-mail:</p>
    <input class="text-input" type="text" name="email" id="email"> </div>
  <div class="row cf">
    <p>Seu Cargo:</p>
    <input class="text-input" type="text" name="cargo" id="cargo"> </div>
  <div class="row cf">
    <p>Seu Telefone/WhatsApp:</p>
    <input class="text-input" type="text" name="phone" id="phone"> </div>
  <div class="row cf">
    <p>Turmas:</p>
    <div class="three col">
      <input type="checkbox" name="turma[]" id="turma1" value="Turma 1">
      <label class="checkbox-label" for="turma1"><span class="icon"><i class="fa fa-pencil" aria-hidden="true"></i></span><span class="text">1 - Turma <br>25/02 a 26/02</span></label>
    </div>
    <div class="three col">
      <input type="checkbox" name="turma[]" id="turma2" value="Turma 2">
      <label class="checkbox-label" for="turma2"><span class="icon"><i class="fa fa-pencil" aria-hidden="true"></i></span><span class="text">2 - Turma <br>25/02 a 26/02</span></label>
    </div>
  </div>
  <div class="row cf">
    <div class="message">Sua Mensagem</div>
    <textarea name="mensagem" id="t1"></textarea>
  </div>
  <row class="cf">
    <input class="submit" type="submit" value="Enviar"> </row>
</form>

And follow the php code

<?php
date_default_timezone_set('America/Sao_Paulo');

$address = "[email protected];";
$data = date("d/m/y"); //pega a data
$ip = $_SERVER['REMOTE_ADDR']; //pega o ip de quem enviou
$hora =  date("d/m/y - H:i:s"); //pega a hora
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$error = false;
$fields = array( 'name', 'email', 'cargo', 'phone');

foreach ( $fields as $field ) {
	if ( empty($_POST[$field]) || trim($_POST[$field]) == '' )
		$error = true;
}

if ( !$error ) {

	$name = stripslashes($_POST['name']);
	$email = trim($_POST['email']);
    $phone= stripslashes($_POST['phone']);
    $cargo= stripslashes($_POST['cargo']);	
    $turma= stripslashes($_POST['turma']);
    $mensagem= stripslashes($_POST['mensagem']);
	
	
	$e_subject = 'DOWNLOAD AGENDA TREINAMENTO -  Como Planejar o Orçamento de RH ';

	//CONTEUDO EMAIL

	$e_body = "\r\nDOWNLOAD AGENDA TREINAMENTO - Como Planejar o Orçamento de RH : $name" . PHP_EOL . PHP_EOL;
	$e_reply = "\r\nE-mail: $email" . PHP_EOL . PHP_EOL;
    $e_phone = "\r\nTelefone/Whats: $phone" . PHP_EOL . PHP_EOL;
	$e_cargo = "\r\nCargo: $cargo" . PHP_EOL . PHP_EOL;	
    $e_turma = "\r\nTurmas: $turma" . PHP_EOL . PHP_EOL;
    $e_mensagem = "\r\nMensagem: $mensagem" . PHP_EOL . PHP_EOL;
    $e_hora = "\r\nData: $hora" . PHP_EOL . PHP_EOL;
    $e_ip = "\r\nIp Usuario: $ip" . PHP_EOL . PHP_EOL;	


	$msg = wordwrap( $e_body . $e_reply .$e_cargo . $e_phone . $e_turma. $e_mensagem. $e_hora . $e_ip , 70 );

	$headers = "From: $email" . PHP_EOL;
	$headers .= "Reply-To: $email" . PHP_EOL;
	$headers .= "MIME-Version: 1.0" . PHP_EOL;
	$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
	$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

	if(mail($address, $e_subject, $msg, $headers)) {

		// EMAIL ENVIADO COM SUCESSO RESPOSTA.

		echo "<script>alert('Obrigado pelo DOWNLOAD da Agenda.');</script><meta http-equiv='refresh' content='0;URL=http://ebscorp.corpbusiness.com.br/'>";

	} else {

		echo 'ERRO!';

	}

}

?>

  • To separate the values you can use implode, thus: implode(",",$_POST['gang']);`

1 answer

0


$turma = stripslashes($_POST["turma"]); //Isso retorna um array


$turma = stripslashes($_POST["turma"][0]); //isso retorna o valor da primeira checkbox se ela estiver marcada

Then change your code to:

$turma = "";
$turma .= stripslashes($_POST["turma"][0]);
if($_POST["turma"][1] != "" && $_POST['turma'][0] != "")
    $turma .= " e ";
$turma .= stripslashes($_POST['turma'][1]);
  • Guy worked right, thank you very much. Both choosing the first class and the second, VLW

Browser other questions tagged

You are not signed in. Login or sign up in order to post.