0

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

          }
        });
  });
})
E_net4
  • 27,810
  • 13
  • 101
  • 139
Olivia A
  • 3
  • 1
  • 1
    Does this answer your question? [How do I get the current date and time in PHP?](https://stackoverflow.com/questions/470617/how-do-i-get-the-current-date-and-time-in-php) – juzraai Dec 09 '20 at 09:50
  • Thanks it does help me about the date()function that I have used in my code in the end. Thank you – Olivia A Dec 09 '20 at 21:10

1 Answers1

0

This passes the "mention" to your 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. 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

        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);
        }
JM-AGMS
  • 1,670
  • 1
  • 15
  • 33
  • Thank you very much! Yes in between I dig further on the date() function. Thanks a lot for providing the link. Much appreciated. And thank you for helping me :) – Olivia A Dec 09 '20 at 21:08