-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathcontact.js
48 lines (39 loc) · 1.22 KB
/
contact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Sending Data to Mail
// Listenig to form
const contactForm = document.querySelector(".contact__form");
async function handleFormSubmit(event) {
event.preventDefault();
const subject = document.querySelector(".subject");
const msgStatus = document.querySelector(".msg__status");
subject.value = "Msg via Naruto Webpage";
const form = event.currentTarget;
const url = form.action;
try {
const formData = new FormData(form);
const fetchOptions = {
method: contactForm.method,
headers: {
Accept: "application/json",
},
body: formData,
};
await fetch(url, fetchOptions);
msgStatus.innerHTML = "Your message has been sent.";
msgStatus.style.display = "block";
msgStatus.style.textAlign = "center";
setTimeout(() => {
msgStatus.style.display = "none";
}, 4000);
form.reset();
} catch (err) {
msgStatus.innerHTML =
"Oops! There was a problem delivering your message, please contact via other means.";
msgStatus.style.display = "block";
msgStatus.style.textAlign = "center";
setTimeout(() => {
msgStatus.style.display = "none";
}, 4000);
form.reset();
}
}
contactForm.addEventListener("submit", handleFormSubmit);