I'm learning how to code and I am setting a webpage with a form so a user can contact me directly into my email adress. At the moment this is what I receive in my email:
Firstname: test,
Name: test,
Email: test@gmail.com,
Phone: 0000000000,
Message: test
I would like to get the date/ time it has been submitted and if possible a mention about the optin that has been consented:
Firstname: test,
Name: test,
Email: test@gmail.com,
Phone: 0000000000,
Message: test ,
date: xx/xx/xx,
time: xx:xx,
Consent: Acceptance of use of data
on my contact.php I have this
<?php
$array = array("firstname" => "", "name" => "", "email" => "", "phone" => "", "message" => "", "firstnameError" => "", "nameError" => "", "emailError" => "", "phoneError" => "", "messageError" => "", "isSuccess" => false);
$emailTo = "myemail@gmail.com";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$array["firstname"] = test_input($_POST["firstname"]);
$array["name"] = test_input($_POST["name"]);
$array["email"] = test_input($_POST["email"]);
$array["phone"] = test_input($_POST["phone"]);
$array["message"] = test_input($_POST["message"]);
$array["isSuccess"] = true;
$emailText = "";
if (empty($array["firstname"]))
{
$array["firstnameError"] = "Il manque votre prénom";
$array["isSuccess"] = false;
}
else
{
$emailText .= "Firstname: {$array['firstname']}\n";
}
if (empty($array["name"]))
{
$array["nameError"] = "Il manque votre nom";
$array["isSuccess"] = false;
}
else
{
$emailText .= "Name: {$array['name']}\n";
}
if(!isEmail($array["email"]))
{
$array["emailError"] = "Ceci n'est pas un email";
$array["isSuccess"] = false;
}
else
{
$emailText .= "Email: {$array['email']}\n";
}
if (!isPhone($array["phone"]))
{
$array["phoneError"] = "Que des chiffres et des espaces, svp...";
$array["isSuccess"] = false;
}
else
{
$emailText .= "Phone: {$array['phone']}\n";
}
if (empty($array["message"]))
{
$array["messageError"] = "Quel est votre message?";
$array["isSuccess"] = false;
}
else
{
$emailText .= "Message: {$array['message']}\n";
}
if($array["isSuccess"])
{
$headers = "From: {$array['firstname']} {$array['name']} <{$array['email']}>\r\nReply-To: {$array['email']}";
mail($emailTo, "Un message de votre site", $emailText, $headers);
}
echo json_encode($array);
}
function isEmail($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function isPhone($phone)
{
return preg_match("/^[0-9 ]*$/",$phone);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
on my js file , I have this
$(function() {
$('#contact-form').submit(function(e) {
e.preventDefault();
var mention = document.getElementById('verif').checked;
if (!mention ) {
console.log(mention);
$("#error").html("<p class='thank-you alert-danger'> Merci d'accepter les conditions</p>")
return false;
}else{
$("#error").empty();
}
$('.comments').empty();
var postdata = $('#contact-form').serialize()
$.ajax({
type:'POST',
url:'php/contact.php',
data: postdata,
dataType: 'json',
success: function(result) {
if(result.isSuccess)
{
$("#contact-form").append("<p class='thank-you'> Votre message a bien été envoyé. Merci de m'avoir contacté </p>")
$("#contact-form")[0].reset();
}
else
{
$("#firstname + .comments").html(result.firstnameError);
$("#name + .comments").html(result.nameError);
$("#email + .comments").html(result.emailError);
$("#phone + .comments").html(result.phoneError);
$("#message + .comments").html(result.messageError);
}
}
});
});
})