derv-net/public/index.js
2025-02-26 20:17:33 -05:00

31 lines
No EOL
697 B
JavaScript

async function formClick(ev) {
ev.preventDefault();
let { target } = ev;
let fData = new FormData(target);
for (let file of target.querySelectorAll('.form-input')) {
file.value = null;
file.textContent = '';
}
let fetched = await fetch(target.action, {
'method': 'POST',
'body': fData
});
let json = await fetched.json();
target.querySelector('.form-message').textContent = json.message;
if (json.redirect && json.success) {
setTimeout(function () {
window.location.href = json.redirect;
}, 2000);
}
}
window.onload = function () {
document.addEventListener('submit', formClick);
}