如何通过表格获得电子邮件提交的日期/时间



我正在学习如何编码,我正在设置一个带有表单的网页,这样用户就可以直接通过我的电子邮件地址与我联系。目前,这是我在电子邮件中收到的内容:

Firstname: test,
Name: test,
Email: test@gmail.com,
Phone: 0000000000,
Message: test 

我想知道它提交的日期/时间,如果可能的话,还想提及已经同意的选项:

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

在我的contact.php上我有这个

<?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']}>rnReply-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;
}

?>

在我的js文件上,我有这个

$(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);
}
}
});
});
})

这会传递"提及";到您的PHP。但你不需要通过,因为如果他们能提交表格,那就意味着他们已经检查过了

var postdata = $('#contact-form').serialize() + '&mention=' + mention;

这会附加";日期时间";以及";同意";到您的电子邮件末尾。Datetime是您将服务器时区设置为的任何值。您可以在PHP文档中阅读更多关于日期和时区的信息:https://www.php.net/manual/en/function.date.php

if($array["isSuccess"]) 
{
$emailText .= "Datetime: " . date('Y-m-d H:i:s') . "n";
$emailText .= "Consent: Acceptance of use of datan";
$headers = "From: {$array['firstname']} {$array['name']} <{$array['email']}>rnReply-To: {$array['email']}";
mail($emailTo, "Un message de votre site", $emailText, $headers);
}

最新更新