pastebin/0cd.xyz-post.js

37 lines
1.0 KiB
JavaScript
Raw Normal View History

function alert() {
let alerts = {
"success": document.getElementById("success"),
"warning": document.getElementById("warning"),
"danger": document.getElementById("danger")
}
return alerts
}
2020-09-26 14:22:19 +00:00
async function post() {
var form = document.getElementById("form");
var alerts = alert();
for (const key in alerts) {
alerts[key].hidden=true
}
2020-09-26 14:22:19 +00:00
let response = await fetch("contact/form", {
method: "POST",
body: new FormData(form)
2020-09-26 14:22:19 +00:00
});
if (response.ok) {
alerts.success.hidden=false
alerts.success.textContent="Message sent, we'll get back to you as soon as possible"
form.reset();
2020-09-26 14:22:19 +00:00
} else {
if (response.status == 400) {
let json = await response.json();
if (json) {
alerts.danger.hidden=false
alerts.danger.textContent=json
2020-09-26 14:22:19 +00:00
}
} else {
alerts.warning.hidden=false
alerts.warning.textContent="Unable to send, please try again later"
2020-09-26 14:22:19 +00:00
}
}
}