I was wondering if there’s a way to prevent a form redirect after a successful form submission. After completing the form, I would like for the user to stay on the same page and not be redirected to a success page. Can this be done by changing the action attribute? Please see my code below.
Based on your onSubmit handler - im assuming this is react.
You can 100 % dictate the behavior of submit handlers through react.
Netlify lets you use their default post submit page, you can also set your own custom page.
You have the action prop listed with no value - by default netlify will route you to their standard success page.
I would try removing action prop, and handling UX through your onSubmit handler
I am finding that i can’t stop the redirect in react. When removing the action attribute it redirects, when I pug empty quotes “” it also redirects. WHen i put a # same.
Only thing I can do is put in a ‘/’ to redirect to my own page, but i want to keep the user on the same scroll distance and not refresh the page back to the top.
Thanks for writing in to the Forums! It looks like it has been a couple days since you posted this. Are you still encountering issues?
If you are still looking for some guidance, we will need a bit more information to help you with this Can you please share your Netlify site name as well as the repo where your code is? Thank you.
I just deployed a new build that removed the / in the action attribute. Could you try refreshing?
Update:
On the second submission (after clicking “back to our site” and re-inputting fields) it works as expected. Seems this is only an issue on first load, and even then if you refresh a couple times and try again you still see default Netlify submission alert.
Yep, still seeing this on other devices and browsers, the site as a service worker, can you please ensure that you have deleted cache and are seeing the most recent commit.
It appears to be working since on both times the form is submitted it successfully gets sent to Netlify. I’m just not too sure why Netlify’s submission page appears on the first time round… that’s why I assumed it was a Netlify Form related issue.
Netlify Forms doesn’t control your JavaScript. If your JavaScript doesn’t call the event.preventDefault() method in time, the form would be submitted like a normal HTML form and would display the form submission page.
I’m not saying test the event listener, I’m saying ask someone else to test because I can’t reproduce the issue. The code you’ve written is correct, but if the browser is still ignoring preventDefault() for some reason, it would be interesting to see if there’s anyone else seeing this too.
Thanks @hrishikesh for the suggestion, I asked someone to test out a submission and they didn’t run into the default page. Do you have an idea to what is causing it to happen only on my machine?
If it’s attached and it’s redirecting, that would be a real problem. In this case, you could go to the sources tab and add a breakpoint at each line in your function to see if any of that is failing by any chance.
If it’s not attaching the event listener, something is going wrong while JavaScript is trying to attach event listeners to all the forms in querySelectorAll.
it’s still weird as to why it’s happening only on your device, but well, maybe you’ve got a cached copy yourself?
I am experiencing the same issue: redirect to default netlify page only on first submission, even with event.preventDefault() at start of handleSubmit. Both on my mobile device and laptop. I am building using NextJS. Will keep you in touch if I found a solution