html - PHP Contact Form GDPR Validation

I have a contact form that I manage to make work.
The form has some fields, the reCaptcha and a consent check box.
The PHP code is like this:
if($_POST)
{
$to_Email = "[email protected]"; //Replace with recipient email address
$subject = 'Form contact'; //Subject line for emails
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type'=>'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]) || !isset($_POST["userprivacy"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'It is necessary to fill in all the fields and validate the terms and conditions box.'));
die($output);
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Subject = $_POST["userSubject"];
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
$customer_privacy = filter_var($_POST["userprivacy"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<3) // If length is less than 3 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(strlen($user_Message)<5) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
$message_Body = "<strong>Name: </strong>". $user_Name ."<br>";
$message_Body .= "<strong>Email: </strong>". $user_Email ."<br>";
$message_Body .= "<strong>Empresa: </strong>". $user_Subject ."<br>";
$message_Body .= "<strong>Message: </strong>". $user_Message ."<br>";
$message_Body .= "<strong>Message: </strong>". $customer_privacy ."<br>";
$headers = "From: " . strip_tags($user_Email) . "\r\n";
$headers .= "Reply-To: ". strip_tags($user_Email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//proceed with PHP email.
/*$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
*/
$sentMail = @mail($to_Email, $subject, $message_Body, $headers);
if(!$sentMail)
{
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hola '.$user_Name .' Gracias por contactanos, en breve nos pondremos en contacto contigo.'));
die($output);
}
}
And the HTML like this:
<form class="getin_form wow fadeInUp">
<div class="col-sm-12" id="result"></div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group bottom45">
<input class="form-control" type="text" placeholder="Nombre" name="user_name" required>
</div>
</div>
<div class="col-md-12 col-sm-12">
<div class="form-group bottom45">
<input class="form-control" type="email" name="user_email" placeholder="Correo Eletronico" required>
</div>
</div>
<div class="col-md-12 col-sm-12">
<div class="form-group bottom45">
<input class="form-control" type="text" name="last_name" placeholder="Empresa">
</div>
</div>
<div class="col-md-12 col-sm-12">
<div class="form-group bottom45">
<textarea class="form-control" name="user_message" placeholder="Mensaje"></textarea>
</div>
<div class="col-md-12 col-sm-12">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="aaaaaaaaaaaaaaaaaaaaaaaaaaa"></div>
</div>
<div>
<div class="col-md-1 checker" id="uniform-customer_privacy">
<input type="checkbox" value="0" required name="customer_privacy" autocomplete="off">
</div>
<div><span>He leído y acepto los <span style="text-decoration: underline;"><strong><a href="http://blablabla.com" target="_blank" rel="noopener">Términos y condiciones</a></strong></span> de blablabla.</span></div>
</div>
<div class="col-sm-12">
<button type="button" class="button defaulthole" id="submit_btn">Enviar</button>
</div>
</div>
</form>
When I fill out the form and press send, I still get the error that it is not completed.
I don't know if I'm putting any variables wrong - I don't really understand why it's not working.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: unable to determine current zabbix database version: the table "dbversion" was not found.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.