Guest User

Untitled

a guest
Mar 8th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener("load", function(){ // Wait for the page to load
  2.  
  3.     "use strict"; // Strict mode for JavaScript
  4.  
  5.     const form = document.querySelector(".feedback") // Get the form
  6.  
  7.     form.addEventListener("submit", function (event){
  8.         event.preventDefault() // Prevent the default action of the form
  9.         let fields = document.querySelectorAll(".feedback .form-control") // Get all the fields
  10.         let valid = true
  11.         for (var i = 0; i < fields.length; i++) {
  12.             fields[i].classList.remove("no-error") // Remove the no-error class from all fields
  13.             if(fields[i].value === ""){ // If the field is empty
  14.                 fields[i].classList.add("has-error")
  15.                 fields[i].nextElementSibling.style.display = "block"
  16.                 valid = false
  17.             }else{ // If the field is not empty
  18.                 fields[i].classList.remove("has-error")
  19.                 fields[i].classList.add("no-error")
  20.                 fields[i].nextElementSibling.style.display = "none"
  21.             }
  22.         }
  23.         if(valid){ // If all the fields are valid
  24.             document.querySelector(".feedback").style.display = "none"
  25.             document.querySelector("#alert").innerText = "Processing your submission, please wait..."
  26.             grecaptcha.ready(function() { // Wait for the recaptcha to be ready
  27.                 grecaptcha
  28.                     .execute("6Lfv9dYkAAAAAFPHgG0CdqbnHGxBOO0WMpnaI4Bx", {
  29.                         action: "feedback"
  30.                     }) // Execute the recaptcha
  31.                     .then(function(token){
  32.  
  33.                         let recaptchaResponse = document.getElementById("recaptchaResponse")
  34.                         recaptchaResponse.value = token // Set the recaptcha response
  35.  
  36.                         fetch("/send.php", {
  37.                             method: "POST",
  38.                             body: new FormData(form), // Send the form data
  39.                         })
  40.                             .then((response) => response.text())
  41.                             .then((response) => {
  42.                                 const responseText = JSON.parse(response) // Get the response
  43.                                 if(responseText.error !== "") { // If there is an error
  44.                                     document.querySelector("#alert").innerText = responseText.error
  45.                                     document.querySelector("#alert").classList.add("error")
  46.                                     document.querySelector(".formfields").style.display = "block"
  47.                                     return
  48.                                 }
  49.                                 document.querySelector("#alert").innerText = responseText.success
  50.                                 document.querySelector("#alert").classList.add("success")
  51.                                 window.location.replace("/thanks") // Redirect to the thanks page
  52.                             })
  53.                     })
  54.             })
  55.         }
  56.     })
  57. })
Add Comment
Please, Sign In to add comment