<script setup lang="ts">
import { ref } from 'vue'
const route = useRoute()
const valid = ref(false)
const email = ref('')
const emailRules = [
(value: string) => {
if (value) return true
return 'E-mail is required.'
},
(value: string) => {
if (/.+@.+\..+/.test(value)) return true
return 'E-mail must be valid.'
}
]
async function handleSubmit() {
if (valid.value) {
const subscriptionSubmission = {
'form-name': 'subscription-form',
email: email.value,
}
// Create a form element
const form = document.createElement('form')
form.method = 'POST'
form.action = `/submission?current_route=${route.path}`
form.style.display = 'none'
form.setAttribute('accept-charset', 'UTF-8') // Optional: If needed
form.setAttribute('enctype', 'application/x-www-form-urlencoded')
// Append form data as hidden input fields
for (const [key, value] of Object.entries(subscriptionSubmission)) {
const input = document.createElement('input')
input.type = 'hidden'
input.name = key
input.value = value as string
form.appendChild(input)
}
// Append form to body and submit
document.body.appendChild(form)
form.submit()
// Cleanup
document.body.removeChild(form)
}
}
</script>
<template>
<v-container fluid class="bg-white d-flex items-center" id="main-container">
<v-row>
<v-col cols="3" offset="5" class="text-center">
<p class="text-h3 anton my-5">STAY UPDATED</p>
<v-form name="subscription-form" ref="form" data-netlify="true" netlify v-model="valid"
@submit.prevent="handleSubmit">
<v-row>
<v-col cols="8">
<v-text-field v-model="email" label="Email Address" class="anton"
variant="outlined" :rules="emailRules" name="email" required></v-text-field>
</v-col>
<v-col cols="4">
<v-btn class="anton" color="black" type="submit" style="height:55px;">SUBSCRIBE</v-btn>
</v-col>
</v-row>
</v-form>
</v-col>
</v-row>
</v-container>
</template>
This is in the footer on my page. Essentially, when I change the action to “/” it works, but if I pass in another path the submission does not work on Netlify’s side of things. I know I am PROBABLY not doing this the right way but was wondering if there is a way using Nuxt3 & Vuetify to submit a form without navigating off that page.
And finally I added the following to my nuxt-config.ts:
routeRules: {
'/': { prerender: true }, # renders the footer form in Netlify Forms
'/{page_with_form_on_it}': { prerender: true }, # renders the specific page form in Netlify Forms
}