Back to Community

How to Auto-Populate Ninja Form Fields Using Query Strings and Cookies

Content

Many Ninja Forms users want to create a more seamless experience by pre-filling form fields with data. This is a common need for scenarios like tracking the source of a submission or personalizing a form for a returning user. Based on community discussions, a frequent question is: "How can I automatically populate Ninja Form fields using a URL query string or data from a user's browser cookie?"

Why This Happens

By default, Ninja Forms fields are blank. Users must manually enter all information, which can lead to lower conversion rates or incomplete data collection. The need to auto-populate fields arises from a desire to streamline the form-filling process, reduce user effort, and pass important contextual data (like a referral source or user meta-data) directly into the form submission.

The Solution: Using Merge Tags

Ninja Forms provides a powerful system of merge tags that can pull data from various sources into your form fields. This functionality is available in the core plugin and does not require paid add-ons for basic use.

1. Auto-Populating from a URL Query String

This is the most straightforward method and is perfect for passing data via a URL. A common use case is adding a ?source=facebook parameter to a link so that the 'source' field is automatically filled with "facebook".

  1. Edit your Ninja Form.
  2. Add a Hidden field to your form (or use a text field if you want the value to be visible).
  3. In the field's Default Value setting, enter the merge tag: {querystring:PARAMETER_NAME}.
  4. Replace PARAMETER_NAME with the name of your URL parameter. For example, to capture ?source=facebook, you would use {querystring:source}.

Pro Tip: Ensure your URL only has one question mark (?). A common pitfall, as seen in the forums, is having a URL with two question marks (e.g., example.com/page?value=1?source=facebook), which will break the query string parsing. Use a single ampersand (&) to add multiple parameters: example.com/page?value=1&source=facebook.

2. Auto-Populating from a User Meta Value (for Logged-In Users)

If you need to pull information from the logged-in user's profile, such as their first name or a custom piece of metadata, you can use the User Meta merge tag.

  1. Edit your Ninja Form.
  2. Add a Hidden or text field to your form.
  3. In the field's Default Value setting, enter the merge tag: {user_meta:YOUR_META_KEY}.
  4. Replace YOUR_META_KEY with the key for the user meta data you want to retrieve. For a user's first name, you would use first_name. For a last name, use last_name.

This method is excellent for pre-filling a form with known information like {user_meta:first_name} {user_meta:last_name}.

Important Limitations and Considerations

  • Cookie Data: The threads indicate that pulling data directly from a browser cookie is not a native feature of Ninja Forms. Achieving this would likely require custom JavaScript development to read the cookie and insert its value into a form field, which is outside the scope of standard support.
  • Complex Logic: For more advanced scenarios, like changing the email recipient based on a populated field value, the 'Ninja Forms – The Contact Form Builder That Grows With You' team suggests using their Conditional Logic add-on.
  • Testing: Always test your form thoroughly after setting up auto-population. Submit test entries and check the resulting emails and submissions to ensure the data is being passed through correctly.

By leveraging these built-in merge tags, you can significantly enhance your form's functionality and user experience without writing a single line of code.

Related Support Threads Support