const form = document.getElementById('form');
const result = document.getElementById('result');
async function handleSubmit(e) {
e.preventDefault();
result.innerHTML = 'Please wait...';
const formData = new FormData(form);
const json = JSON.stringify(Object.fromEntries(formData));
const res = await fetch('https://api.dev-forms.com/v1/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: json,
});
if (!res.ok) {
result.innerHTML = 'Something went wrong!';
}
const data = await res.json();
if (!data.success) {
console.log(data.body.message);
result.innerHTML = 'Something went wrong!';
return;
}
result.innerHTML = 'Thanks for contacting us!';
form.reset();
}
if (form && result) {
form.addEventListener('submit', handleSubmit);
}