Change redirect after form submission | Community
Skip to main content

Change redirect after form submission

  • November 28, 2022
  • 17 replies
  • 0 views

Because we can't use Zendesk forms without enabling the Help Center (which is not yet ready), I am trying to remove from our form all references/links to the Help Center.  I've edited the theme code to remove the breadcrumbs and the logo hyperlink.  I think all that is left to do is to stop the form from redirecting users back to the Help Center after they submit a request.  

How can I change where users are directed after they submit a request?

17 replies

Zendesk13
  • November 29, 2022

Hi @kevin39, one way to do it is to add the following Javascript to your document_head.hbs file in your Guide theme.

<!-- Redirect user after form submission -->  
<script>
if (document.referrer.match('/requests/new')) {
  window.location.href = 'URL';
}
</script>

All you have to do is replace "URL" with the real URL you want to redirect your users to.

Hope this helps!


  • Author
  • November 29, 2022

That is exactly what I needed.  Thank you so much, Pedro.


Zendesk13
  • November 29, 2022

You're welcome, I'm happy to help!


  • January 18, 2023

Hi Pedro,

I think this is exactly what I need, but when I added the code to document_head.hbs, it now redirects to the correct page when I select a form (before having the chance to enter any information). Is there a way for the user to select the form, fill it out, submit the form, then redirect to a specific page?

Thanks!


Zendesk13
  • January 20, 2023

Hi @amy25, thank you for that heads-up, the code should indeed prevent that situation! I just tried this in my account and it worked without any issues:

<script>
if (document.referrer.match('/requests/new') && (window.location.href.indexOf("/requests/new?ticket_form_id=") === -1)) { 
  window.location.href = 'URL';
}
</script>

Cheers!


  • January 20, 2023

Hi Pedro,

It redirects after the user submits the form, which is fantastic! 

I am noticing, however, that it redirects every time the form is submitted, even if required fields are left blank. So instead of seeing the notices that the form cannot be submitted when XYZ field is blank, I am redirected automatically and don't realize that the form wasn't actually submitted. I checked the Support agent workspace and confirmed that tickets are only created when those required fields are filled in. Is there a way to redirect only after a successful submission?


  • February 10, 2023

@pedro17 Is there any way to do this for a single form when there are multiple? 

Editing to add @ifra...would you happen to know how to adjust? 

I did try this code on the document_head.hbs page: 

<script>
if (document.referrer.match('/requests/new') && (window.location.href.indexOf("/requests/new?ticket_form_id=[form id number]") === -1)) {
  window.location.href = 'https://zendeskformlink';
}
</script>

Ifra
  • February 14, 2023

Nicole, I tried for single form but couldn't get it.


  • February 14, 2023

@ifra thank you so very much for trying! I didn't have luck either with trying to limit it to a single form. 


Ifra
  • February 14, 2023

I have been doing it for an hour, applying ideas but couldn't get it done perfectly.


Zendesk13
  • May 19, 2023

Hi @amy25, sorry for taking such a long time to reply! This worked for me:

<script>
if (document.referrer.match('/requests/new') 
  && (window.location.href.indexOf("/requests/new?ticket_form_id=") === -1)
  && (document.referrer.match !== window.location.href.indexOf)) 
  && (document.referrer.indexOf("/requests/new?ticket_form_id=") === -1) { 
  window.location.href = 'URL';
}
</script>

That is, if one tries to submit the form without filling in the required fields, the redirect doesn't occur.


  • May 19, 2023

Thank you, Pedro Rodrigues! Do you know why clicking on the company logo at the top left corner takes me to the redirect page?

That is, on all pages, clicking on the top left logo takes me to the Help Center home page (expected). But after I click "Submit a request", any clicks on the logo take me to the redirect page that should be reserved for fully submitted tickets (not expected). I would anticipate that no matter where I am in the form process, clicking on the company logo will take me back to the Help Center home page... not the redirect page that tells me I've successfully submitted a ticket (especially since I can do this without even selecting a form to fill out, let alone submit it).


Zach36
  • June 24, 2023

Has anyone set this up across multiple forms? I want to redirect users based on the specific form they fill out...


Nicole17
  • July 12, 2023
ß

  • August 29, 2023

Hi! This is working for me however it ignores the required fields of the form. Is there additional code that I can use?


Zach36
  • August 30, 2023

@zendesk13

I've implemented your script

<script>
if (document.referrer.match('/requests/new') 
  && (window.location.href.indexOf("/requests/new?ticket_form_id=") === -1)
  && (document.referrer.match !== window.location.href.indexOf)) 
  && (document.referrer.indexOf("/requests/new?ticket_form_id=") === -1) { 
  window.location.href = 'URL';
}
</script>

However, whenever someone tries to submit a form without a mandatory field, it edits the url to no longer include the "new?ticket_form_id=[form_id]" and instead maintains the url subdomain.zendesk.com/hc/en-us/requests which then ignores the redirect logic of the script... Any ideas on a solution?

EDIT: The redirect is no longer happening. Instead submitting a form is going to:

subdomain.zendesk.com/hc/en-us?return_to=%2Fhc%2Frequests

EDITx2: I went in and stripped everything back and this seems to work perfectly (both when the form is filled out correctly & when a user misses a mandatory field)

<script>

if (window.location.href === "https://SUBDOMAIN.zendesk.com/hc/en-us?return_to=%2Fhc%2Frequests")

{window.location.href = ‘REDIRECT_LINK’;}

</script>

  • September 8, 2023

Hello there,

Starting from @zach36 EDITx2, I've modified it a bit so that it works for multilanguage :

<script>
if (
    window.location.href.includes("?return_to=%2Fhc%2Frequests")
) {
  window.location.href = "URL";
}
</script>