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