Implemented 'custom forms' to replace individual scripts for deposit and supply forms

This commit is contained in:
Julian Müller (ChaoticByte) 2023-04-14 20:48:01 +02:00
parent 5ab0d1088f
commit 2bab323b86
4 changed files with 15 additions and 54 deletions

View file

@ -0,0 +1,34 @@
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);
});
});