Search code examples
phpformspostcheckbox

How can I make my HTML form email me when a checkbox is checked using PHP POST?


I am trying to add a checkbox to a form and get the confirmation through an email. I am using the POST method. All the rest of fields works fine, but checkbox doesn't...

Thanks!

HTML FORM

<form class="form-contact" id="formulario" name="contacto" method="post" action="paraenviar.php">

<label for="nombre">Nombre</label>
<input name="nombre" id="nombre" type="text" required class="fill" size="20" /><br /> 
      
<label for="empresa">Empresa</label>
<input name="empresa" id="empresa" type="text" required class="fill" size="20" /><br /> 

<label for="email">eMail</label>
<input name="email" id="email" type="text" required class="fill" size="20" /><br /> 

<label for="mensaje">Mensaje</label>
<textarea name="mensaje" id="mensaje" cols="26" rows="3" required class="textarea"></textarea><br />
        
<input type="checkbox" id="protecciondatos" name="protecciondatos" value="protecciondatos" required>

<label for="proteccion-datos">He leído y acepto la <a href="#">política de protección de datos</a> </label>
        
<br><br>
        
<input class="sendbutton" name="insert" type="submit" value="Enviar"/>

</form>

PHP

<?php

$nombre = $_POST['nombre'];
$empresa = $_POST['empresa'];
$email = $_POST['email'];


$mensaje = "NOMBRE: " . $nombre ." \r\n";
$mensaje .= "EMPRESA: " . $empresa . " \r\n";
$mensaje .= "E-MAIL: " . $email . " \r\n";
$mensaje .= "MENSAJE: " . $_POST['mensaje'] . " \r\n";
$mensaje .= "FECHA DEL ENVÍO: " . date('d/m/Y', time());

$para = 'xxxx@xxx';
$asunto = 'Blablabla';

mail($para, $asunto, utf8_decode($mensaje));

?>

Solution

  • First of all, as someone commented, you don't actually get the value from the form.

    As of your question, your php code will be

    <?php
    
    $nombre = $_POST['nombre'];
    $empresa = $_POST['empresa'];
    $email = $_POST['email'];
    $protecciondatos=$_POST['protecciondatos'];
    
    
    $mensaje = "NOMBRE: " . $nombre ." \r\n";
    $mensaje .= "EMPRESA: " . $empresa . " \r\n";
    $mensaje .= "E-MAIL: " . $email . " \r\n";
    $mensaje .= "MENSAJE: " . $_POST['mensaje'] . " \r\n";
    $mensaje .= "FECHA DEL ENVÍO: " . date('d/m/Y', time());
    
    $para = 'xxxx@xxx';
    $asunto = 'Blablabla';
    
    if($protecciondatos=="protecciondatos"){
       $hasAccepted="User accepted terms and conditions";
       $mensaje .= " ".$hasAccepted;
       mail($para, $asunto, utf8_decode($mensaje));
    }
    else{
       //error message, or whatever
    }
    
    ?>
    

    An even better way is to put everything in the if statement, like so:

    <?php
        $protecciondatos=$_POST['protecciondatos'];
        if($protecciondatos=="protecciondatos"){
            $nombre = $_POST['nombre'];
            $empresa = $_POST['empresa'];
            $email = $_POST['email'];
    
            $mensaje = "NOMBRE: " . $nombre ." \r\n";
            $mensaje .= "EMPRESA: " . $empresa . " \r\n";
            $mensaje .= "E-MAIL: " . $email . " \r\n";
            $mensaje .= "MENSAJE: " . $_POST['mensaje'] . " \r\n";
            $mensaje .= "FECHA DEL ENVÍO: " . date('d/m/Y', time());
            
            $para = 'xxxx@xxx';
            $asunto = 'Blablabla';
            $hasAccepted="User accepted terms and conditions";
            $mensaje .= " ".$hasAccepted;
            mail($para, $asunto, utf8_decode($mensaje));
        }
        else{
            //error message, or whatever
        }     
    ?>
    

    Also, change

    <input type="checkbox" id="protecciondatos" name="protecciondatos" value="protecciondatos" required>
    
    <label for="proteccion-datos">He leído y acepto la <a href="#">política de protección de datos</a> </label>

    to

    <input type="checkbox" id="protecciondatos" name="protecciondatos" value="protecciondatos" required>
    
    <label for="protecciondatos">He leído y acepto la <a href="#">política de protección de datos</a> </label>

    As it's not correct.