Hi,
I’ve seen a lot of questions regarding this online, but no one seems to have a solution.
I’m trying to get form submission to work with a Nuxt 3 app. I have validation so I’ve been using the AJAX approach. However, I have tried this without validation as well and it still doesn’t seem to work
I’m using vuestic for css and vuelidate for form validation, pushed up site with nuxt build (nuxt generate results in 404)
Form top:
<form
@submit.prevent="submitForm"
name="contact"
method="post"
action="/thanks"
netlify
netlify-honeypot="bot-field"
>
<input type="hidden" name="form-name" value="contact" />
<p hidden>
<label>Don’t fill this out: <input name="bot-field" /></label>
</p>
Form object:
const formData = reactive({
name: "",
formEmail: "",
phone: "",
subscription: "",
message: "",
});
submit function
const submitForm = async () => {
const isValid = await v$.value.$validate();
if (!isValid) {
console.log("form has errors");
return true;
}
let body = new URLSearchParams(formData).toString();
console.log("body", body);
fetch("/", {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: body,
})
.then((res) => {
console.log("form sumbitted", res);
if (res.status === 200) {
router.push({ path: "/thanks" });
}
})
.catch((error) => console.error(error));
};
index.html in root
<form name="contact" action="/thanks" netlify hidden>
<input type="hidden" name="form-name" value="contact" />
<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="phone" />
<input name="subscription"></input>
<textarea name="message"></textarea>
</form>
Things I’ve tried:
-
wrapped form in ClientOnly
-
submit form without validation
-
removed hidden input
-
used data-netlify=“true” instead of netlify in form
Success message:
Response {type: 'basic', url: 'https://communityui.com/', redirected: false, status: 200, ok: true, …}
body
:
(...)
bodyUsed
:
false
headers
:
Headers {}
ok
:
true
redirected
:
false
status
:
200
statusText
:
""
type
:
"basic"
url
:
"https://communityui.com/"
[[Prototype]]
:
Response
body value being sent:
name=test+name&formEmail=test%40example.com&phone=&subscription=Premium&message=Hi+this+is+a+test
The only other thing I can think of that might be causing a problem is in the index.html file maybe the elements don’t match exactly because i’m using a css library? I’m not sure if that matters if the names match exactly.
I’m officially completely stuck any help would be appreciated.