Redirect after successfully submitting form
Hi there,
I've created a custom 'successful submission' page that should appear after a user has filled out and submitted a form. Now I need to create a redirect from a successfully completed form to that custom page.
The following code has been recommended to me, and it works whenever the user submits the form. The problem is that it works EVERY time, including when there are errors (e.g., required fields are blank) and the form is not actually submitted. That is, the user can 'submit' a blank form which is not actually sent, and they would still be redirected to the 'successful submission' page.
Can someone please recommend a way to redirect to the custom page only after the form is actually submitted?
<script>
if (document.referrer.match('/requests/new') && (window.location.href.indexOf("/requests/new?ticket_form_id=") === -1)) {
window.location.href = 'URL';
}
</script>
Thanks!
-
Hi Amy Giella,
I tested the following modification on Copenhagen 2.18.0
<script>
if (document.referrer.match('/requests/new') && (window.location.href.indexOf("return_to=%2Fhc%2Frequests") !== -1)) {
window.location.href = 'URL';
}
</script>Basically I noticed that the theme has a default redirect to guide home with the query param return_to=%2Fhc%2Frequests, so you can look out for it and redirect the end user.
Hope that helps.
-
Hi Ahmed Zaid,
Thank you for taking a look at this! I used this in the document.head.hbs after replacing "URL" with the custom page's URL. It does make the user fill in all the required fields before submitting the form, but instead of taking me to the custom page, I am taken back to the main home page. Am I maybe adding the code to the wrong place? I'm not sure what else to try.
-
Hi Amy,
It works for me whether I add it to the top of the header template or directly in the script.js file. Can you try adding the url as
/hc/{{help_center.locale}}/p/your_page_title
Replace only your_page_title
-
Great, this works for me however it bypasses the required fields. What else can I do to ensure when submitting it won't bypass the required fields so the user must fill out the required fields first and then submit the form, which still redirects?
-
Hi Brittany Loy,
I am not sure how it bypasses the required fields. Can you share more details about the exact steps you took?
Please sign in to leave a comment.
5 Comments