Hello,
I have been reading the doc(Forms setup | Netlify Docs) and this post here ([Support Guide] Form problems, form debugging, 404 when submitting - #30 by fool), but still having problems. I am hoping someone can spot an error.
I have an html form on a static site
<form id="lead-magnet" name="lead-magnet" action="/" netlify>
I am posting the request with javascript because I want to trigger other events on submit (file download).
const encode = (data) => {
return Object.keys(data).map((key) => encodeURIComponent(key) + '=' + encodeURIComponent(data[key])).join('&');
};
const processForm = (form) => {
const data = new FormData(form);
fetch('/', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: encode({ 'form-name': 'lead-form', ...data })
})
.then(() => {
console.log('Form has been submitted!');
downloadFile()
})
.catch((error) => {
console.log('error', error);
});
};
const leadForm = document.getElementById('lead-magnet');
if (leadForm) {
leadForm.addEventListener('submit', (e) => {
processForm(leadForm);
e.preventDefault();
});
}