How can I dynamically redirect to a CMS created after form submission in Webflow?

TL;DR
  • Set up a CMS collection with items for redirection and identify unique identifiers for URLs.
  • Design a form with fields matching CMS slugs; add JavaScript to handle form submission, prevent default action, and redirect to constructed URLs. 
  • Test by submitting the form to ensure correct redirection to CMS pages.

To dynamically redirect a user to a CMS page after a form submission in Webflow, you'll need to use a combination of your form, CMS collection, and JavaScript. This process can provide a personalized user experience by redirecting users to specific pages based on their input.

1. Set Up Your CMS and Get Collection Information

  • Create or ensure you have a CMS collection with items that users should be redirected to.
  • Make note of any unique identifiers or slug fields that can be used as part of the URL for redirection.

2. Design Your Form

  • Add a form element on your page where users can submit their information.
  • Include a form field that matches the unique identifier or slug in the CMS that will determine the redirect URL.

3. Add Custom JavaScript for Redirection

  • Access the page settings where your form is located in Webflow.
  • Insert custom JavaScript in the "Before </body> tag" section to handle the redirection after form submission:

  • Attach an event listener to the form's submit event.
  • Prevent the default form submission.
  • Retrieve the form field data used for identifying the CMS item.
  • Construct the CMS item's URL using the identifier or slug.
  • Redirect the user to the constructed URL.

4. Example JavaScript

  • Example Function: Here’s how your JavaScript might look. Note that field names and URLs should be adapted to your specific setup:

  ```javascript

  document.getElementById('your-form-id').addEventListener('submit', function(event) {

    event.preventDefault();

    const slug = document.querySelector('input[name="your-field-name"]').value;

    const redirectUrl = /collection-slug/${slug};

    window.location.href = redirectUrl;

  });

  ```

5. Test Your Setup

  • Submit the form with a value that corresponds to a CMS item's identifier.
  • Ensure the user is redirected to the correct CMS page.

Summary

To redirect users dynamically to a CMS page in Webflow after form submission, set up a form that collects the necessary CMS identifier, and use JavaScript to capture the form submission event. Then, prevent the default submission action, construct a URL with the identifier, and set window.location.href to this new URL for redirection.

Rate this answer

Other Webflow Questions