drinks-manager/static/js/custom_form.js

35 lines
1.5 KiB
JavaScript
Raw Normal View History

document.addEventListener("DOMContentLoaded", () => {
// elements
let customForm = document.getElementById("customform");
let statusInfo = document.getElementById("statusinfo");
let submitButton = document.getElementById("submitbtn");
// event listener for deposit form
// this implements a custom submit method
customForm.addEventListener("submit", (event) => {
submitButton.disabled = true;
event.preventDefault(); // Don't do the default submit action!
let xhr = new XMLHttpRequest();
let formData = new FormData(customForm);
xhr.addEventListener("load", (event) => {
status_ = event.target.status;
response_ = event.target.responseText;
if (status_ == 200 && response_ == "success") {
statusInfo.innerText = "Success. Redirecting soon.";
window.location.replace("/");
}
else {
statusInfo.classList.add("errortext");
statusInfo.innerText = "An error occured. Redirecting in 5 seconds...";
window.setTimeout(() => { window.location.replace("/") }, 5000);
}
})
xhr.addEventListener("error", (event) => {
statusInfo.classList.add("errortext");
statusInfo.innerText = "An error occured. Redirecting in 5 seconds...";
window.setTimeout(() => { window.location.replace("/") }, 5000);
})
xhr.open("POST", customForm.action);
xhr.send(formData);
});
2023-02-17 22:01:09 +01:00
});