LOPD acceptance box in form and php

1

we have to include in the contact form the acceptance box of the privacy policy, that in html is simple with this code

<input type="checkbox" name="aceptar_terminos" id="aceptar_terminos" value="aceptar_terminos"  /> He leído y acepto la <a href="#" target="_blank">Política de Privacidad</a>

What should be included in the php so that if that box is not selected, the form can not be sent?

this is the php we have now

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = '[email protected]';
$subject = 'Mensaje desde la web'.$field_name;

$body_message = 'De: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Mensaje: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Gracias. Nos pondremos en contacto lo antes posible.');
        window.location = 'contactar-con-catoira-camp-campamento-de-verano.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Hmmm. Algo ha fallado en el formulario de contacto. Mande por favor un correo a [email protected]');
        window.location = 'contactar-con-catoira-camp-campamento-de-verano.html';
    </script>
<?php
}
?>

Thanks for the help

    
asked by Antonio 23.05.2018 в 14:00
source

1 answer

3

Instead of sending the form to the server and then checking, you can do it by Javascript on the client's side. That way, you save unnecessary loads to the server.

To do this, you would make the Enviar button depend on the status of the acceptance of the terms or not. That is, as long as the terms are not accepted, the Enviar button will remain deactivated and consequently the form can not be sent. If you notice, that's how it usually works.

It is simply a matter of listening to the event change of the terms acceptance checkbox. The function will check if the checkbox is selected or not, and based on that will change the status of the button or not. That happens every time the user dials or unchecks the box, as you can see.

It would be more or less like this:

document.getElementById("aceptar_terminos").addEventListener('change', checkAccepted);

function checkAccepted(event) {
  var btnEnviar = document.getElementById("btnEnviar");
  console.log(this.checked);
  var isNotChecked = !this.checked;
  btnEnviar.disabled = isNotChecked;

}
<form id="form1" action="#">
  <input id="ibxNombre" type="text" placeholder="Nombre" />
  <br />
  <input id="ibxApellido" type="text" placeholder="Apellido" />
  <br />
  <input id="ibxMail" type="email" placeholder="@email" />
  <br />
  <input type="checkbox" name="aceptar_terminos" id="aceptar_terminos" value="aceptar_terminos" /> He leído y acepto la <a href="#" target="_blank">Política de Privacidad</a>
  <br />
  <input type="submit" id="btnEnviar" disabled>
</form>
    
answered by 23.05.2018 в 15:17