    <div id="form">
      <h2>
          Je m'engage
      </h2>

        <!-- Making a text input -->

        <input type="text" id="nom" required="" size="30" maxlength="40" placeholder="Votre Prénom et votre NOM"> 
&nbsp;&nbsp;<input type="email" id="email" required="" size="20" maxlength="40" placeholder="Votre email"> 

        <!-- Button to send data -->
        <button  style="background:red; color:white" onclick="sendJSON()">  
            Envoyer  
        </button> 

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



  <!-- Include the JavaScript file -->
  <script>
      var nom;
      var email;

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


      function sendJSON(){ 

             email = document.querySelector('#email');
             nom = document.querySelector('#nom');


            //if(validateEmail(email.value)) {
                    sendEmail();
            //}
            //else 
             //  alert("Attention votre email n'est pas conforme.");



       }   

       function sendEmail(){ 

           let result = document.querySelector('.result'); 
           // 
            // 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 
                    result.innerHTML = this.responseText;
                    document.querySelector("#form").style.display = "none";

                } 
            }; 
 
         let tab=[];
             tab.push({
                 name : nom.value,
                 email : email.value,
             });
             var data = JSON.stringify(tab); 

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

  </script> 

