How can I prefill a form field with information from the URL in Webflow, specifically for unsubscribes using a passed email, so that the user only needs to click the submit button?

TL;DR
  • Add your form to Webflow with a specific field for the email address.
  • Use a URL parameter (e.g., ?email=user@example.com) to pass the email.
  • Insert custom JavaScript in Webflow's Page Settings to extract the URL parameter and autofill the form field.
  • Publish and test the site to ensure the email field prefills appropriately.

To prefill a form field in Webflow with information from the URL, you'll use a combination of Webflow and a little bit of custom JavaScript.

1. Set Up Your Webflow Form

  • Add your form to your Webflow page where you want users to unsubscribe.
  • Ensure you have a form field specifically for the email address.

2. Identify the URL Parameter

  • Decide on the URL parameter you'll use to pass the email (e.g., ?email=user@example.com).

3. Create the Custom JavaScript

  • Open Page Settings in your Webflow Designer and scroll down to the Before </body> tag section.
  • Add the following script:
  • Extract URL parameter: The script retrieves the email from the URL parameter.
  • Fill the form field: The script automatically fills the specified form field with the extracted email.

document.addEventListener('DOMContentLoaded', function() {
    // Function to get URL parameter
    function getQueryParam(param) {
        var params = new URLSearchParams(window.location.search);
        return params.get(param);
    }
    // Get email from URL
    var email = getQueryParam('email');
    // If email exists, prefill the form field
    if(email) {
        var emailInput = document.querySelector('input[name="Email"]'); // Replace with your field's name
        if(emailInput) {
            emailInput.value = email;
        }
    }
});

4. Test Your Implementation

  • Publish your site and navigate to the page with the appropriate URL (e.g., yourwebsite.com/unsubscribe?email=user@example.com).
  • Verify that the email field is prefilled and only requires the user to click the submit button.

Summary

To prefill a form field with URL parameters in Webflow, modify your form to capture the desired data, use custom JavaScript to detect the URL parameter (e.g., email), and automatically fill the form field based on this information. This streamlines the unsubscribe process by allowing users to simply click submit.

Rate this answer

Other Webflow Questions