简体   繁体   English

通过表单提交 email 的日期/时间?

[英]How to get the date/time of the email when it has been submitted through the the form?

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.我正在学习如何编码,并且正在设置一个带有表单的网页,以便用户可以直接通过我的 email 地址与我联系。 At the moment this is what I receive in my email:目前这是我在 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:我想获得提交的日期/时间,如果可能的话,请提及已同意的 optin:

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 我有这个

<?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在我的 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);
              }

          }
        });
  });
})

This passes the "mention" to your PHP.这会将“提及”传递给您的 PHP。 But you don't need to pass it because if they can submit the form, that means they checked it.但是你不需要通过它,因为如果他们可以提交表单,那就意味着他们已经检查过了。

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

This appends "Datetime" and "Consent" to your end of your email message.这会将“日期时间”和“同意”附加到 email 消息的末尾。 Datetime is whatever you have server timezone set to.日期时间是您将服务器时区设置为的任何值。 You can read more about date and timezones on the PHP documentation: https://www.php.net/manual/en/function.date.php您可以在 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 data\n";
            $headers = "From: {$array['firstname']} {$array['name']} <{$array['email']}>\r\nReply-To: {$array['email']}";
            mail($emailTo, "Un message de votre site", $emailText, $headers);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM