Hi all,
I am trying to set up an email newsletter form. I want to update the DOM (success message), without a page reload. At the same time, on the backend, a POST request is issued to add this email to my EmailOctopus list (via their API).
My site is built with Gridsome (Vue Static Builder). To prevent redirect to success page I add @submit.prevent attribute to my form:
<form
name="add-subscriber"
id="myForm"
method="post"
data-netlify="true"
data-netlify-honeypot="bot-field"
@submit.prevent="handleFormSubmit">
<input type="hidden" name="form-name" value="add-subscriber" />
<input type="email" v-model="userEmail" name="user_email" required="" id="id_user_email">
<button type="submit" name="button">Subscribe</button>
</form>
This is how my handleFormSubmit function currently looks like:
handleFormSubmit() {
document.getElementById("myForm").innerHTML = `
<div>
Almost there! Check your inbox for a confirmation e-mail.
</div>`
Now the dom gets updated with a success message, but the form is not actually submitted. Clearly I need to add something else to my function.
Previously I had a POST request with a â/â URL, but I kept getting a Cannot POST /
error:
handleFormSubmit() {
document.getElementById("myForm").innerHTML = `
<div>
Almost there! Check your inbox for a confirmation e-mail.
</div>`
await axios.post('/', {
data: {
"user_email": this.userEmail,
"apiKey": apiKey
}
})
.then(data => console.log(data))
.catch(error => console.log(error))
}
My function is called submission-created.js, so it can be launched only when the form submission is registered with Netlify.
My site is currently located at https://rk-gridsome-pw.netlify.com/
Sorry for the long message. I hope this is clear. If further details are needed please let me know. Thanks a ton in advance. I really appreciate everyoneâs help!
Best,
Rasul
P.S. I have tried to use Netlify Tutorial and run into a couple of issues described here in a stackoverflow post