I’m trying to use netlify forms with my Next.js website but it doesn't work, I don’t get how to do it. Please help!
Here's my code.
<form className='contacts-form' onSubmit={handleSubmit} method='post' name="contact" data-netlify="true">
<input type="hidden" name="form-name" value="contact" />
{/* other inputs... */}
<button type="submit" name="submit">Submit</button>
</form>
const handleSubmit = (event) => {
event.preventDefault();
const myForm = event.target;
const formData = new FormData(myForm);
fetch("/", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams(formData).toString(),
})
.then(() => router.push("/thank-you"))
.catch((error) => alert(error));
};
I solved my issue.
I had an error 303 due to the redirect of my app. I solved it by editing my handleSubmit function: Instead of
I changed it to:
because previously I had
!!!You need a form.html to help netlify detect your form!!! You have to put it in the public folder:
Note that if labels are empty, Netlify will not send the content of the input to the email notification. It will only be shown in the Netlify UI.