<h2>
Adresse mail pour nous contacter : carhaix2020.bzh@gmail.com</li>
</h2>
<br>&nbsp;
<br>&nbsp;
 <div id="form2">
      <h2>
          Nous contacter par formulaire :
      </h2>

        <!-- Making a text input -->

        <input type="text" id="nom2" required="" size="30" maxlength="40" placeholder="Votre Prénom et votre NOM"> 
        <br><input type="email" id="email2" required="" size="20" maxlength="40" placeholder="Votre email"> 
        <br><textarea id="texte2" rows="10" cols="40" placeholder="Veuillez saisir votre message..." required=""></textarea>
        <br><!-- Button to send data -->
        <button  width="200px" style="background:red; color:white" onclick="sendJSON2()">  
            Envoyer  
        </button> 

      </div>
      <!-- For printing result from server -->
      <p id="result2" style="color:green"></p> 



  <!-- Include the JavaScript file -->
  <script>
      var nom2;
      var email2;
      var texte2;

      function validateEmail2(email) 
      {
           return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
      }


      function sendJSON2(){ 

             email2 = document.querySelector('#email2');
             nom2 = document.querySelector('#nom2');
             texte2 = document.querySelector('#texte2');


            if(validateEmail2(email2.value)) {
                    sendEmail2();
            }
            else 
               alert("Attention votre email n'est pas conforme.");



       }   

       function sendEmail2(){ 

           let result2 = document.querySelector('#result2'); 
           // 
            // Creating a XHR object 
            let xhr = new XMLHttpRequest(); 
            let url = "./js/submit.php"; 

            // open a connection 
            xhr.open("POST", url, true); 

            // Set the request header i.e. which type of content you are sending 
            xhr.setRequestHeader("Content-Type", "application/json"); 

            // Create a state change callback 
            xhr.onreadystatechange = function () { 
                if (xhr.readyState === 4 && xhr.status === 200) { 

                    // Print received data from server 
                    result2.innerHTML = this.responseText;
                    document.querySelector("#form2").style.display = "none";

                } 
            }; 

            // Converting JSON data to string 
           let tab=[];
             tab.push({
                 name : nom2.value,
                 email : email2.value,
                 texte : texte2.value
             });
           // alert(JSON.stringify(tab));
           //var data = JSON.stringify({ "name": nom.value, "email": email.value , "texte": texte.value }); 
             var data = JSON.stringify(tab); 


            // Sending data with the request 
            xhr.send(data); 
}

  </script> 