TanchoToplineCargo/public/sunsarioverseas/js/partials/contact.js

51 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-05-05 04:47:49 +00:00
/* ===============================================
CONTACT FORM
=============================================== */
const form = document.getElementById("form");
const result = document.getElementById("result");
form.addEventListener("submit", function (e) {
const formData = new FormData(form);
e.preventDefault();
var object = {};
formData.forEach((value, key) => {
object[key] = value;
});
var json = JSON.stringify(object);
result.innerHTML = "Please wait...";
fetch("https://api.web3forms.com/submit", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json"
},
body: json
})
.then(async (response) => {
let json = await response.json();
if (response.status == 200) {
result.innerHTML = json.message;
result.classList.remove("text-gray-500");
result.classList.add("text-green-500");
} else {
console.log(response);
result.innerHTML = json.message;
result.classList.remove("text-gray-500");
result.classList.add("text-red-500");
}
})
.catch((error) => {
console.log(error);
result.innerHTML = "Something went wrong!";
})
.then(function () {
form.reset();
setTimeout(() => {
result.style.display = "none";
}, 5000);
});
});