-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.js
34 lines (33 loc) · 1012 Bytes
/
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
var form = document.getElementById("my-form");
async function handleSubmit(event) {
event.preventDefault();
var status = document.getElementById("my-form-status");
var data = new FormData(event.target);
fetch(event.target.action, {
method: form.method,
body: data,
headers: {
Accept: "application/json",
},
})
.then((response) => {
if (response.ok) {
status.innerHTML = "Thanks for your submission! I will reply Soon.";
form.reset();
} else {
response.json().then((data) => {
if (Object.hasOwn(data, "errors")) {
status.innerHTML = data["errors"]
.map((error) => error["message"])
.join(", ");
} else {
status.innerHTML = "Oops! There was a problem submitting your form";
}
});
}
})
.catch((error) => {
status.innerHTML = "Oops! There was a problem submitting your form";
});
}
form.addEventListener("submit", handleSubmit);