You are here

Webforms in Drupal: passing information through the URL

The use case

Have you ever created a webform in Drupal and wished you could pass some info into it?  Perhaps you want to capture something about the user who's filling out the form or where they're coming from or other similar information that may be able to be passed in automatically.

Fear not!  It's quite easy to pass information into Drupal's webforms through URLs.  Just grab the variables from the URL, set them as defaults into your necessary webform fields, and even make these fields editable by the user, disabled (but visible) or hidden altogether.  That should fit just about any need you have.

In my case, I want to create a generic 'Contact' form, which my customers will use to contact service providers from a list generated by a view.

When someone clicks on the contact webform's URL from a service provider in my view, however, I want to capture the person they are trying to contact, so that's the piece I'll pass along via the URL.

Data flow

Here is the view of service providers that can be contacted.  The little envelope/mail icon is simply a link that goes to the contact webform below.

And here is the generic contact webform: (As you can see, there is a special field at the top, which is auto-filled through the URL, displaying which service provider we're trying to contact).

 

 

Passing the data through the URL

First thing's first.  When we link that mail icon to the webform URL, which is just /contact-provider, we need to pass in the name of the service provider in that particular row.

So, in the view field where I am generating the icon, I have entered the following into the 'Rewrite results' section

<a href="/contact-provider?service_name=[title_1]" target="_blank">
  <img src="/sites/all/themes/custom/my_theme/images/global/icons/mail-envelope-icon.png" />
</a>

And that's all it is!  The URL variable 'service_name' will be set to whatever is in [title_1] - that's just the node's title and it contains the name of the service provider.  You can look at your own Replacement patterns and use any of the fields added to your view so far to pass in as variables.

Test it

Before we set our webform to read that URL, let's go ahead and test to make sure it's really getting populated correctly.

Simply save your view (or do this in the view's preview section, if you have that enabled), hover over your link (in my case, an image) and see if your browser is displaying what you'd expect.

 

Accept the URL data in the webform

Now, in the webform that the URL /contact-provider points to, we have to actually accept and store that 'service_name' parameter coming through the URL.  It's easy.

Simply add a new text field - I'll call it 'Service provider' - and set its default value to that URL parameter in the field settings:

I have webforms that accept multiple parameters from the URL in both hidden and visible fields.  Just use the syntax above: %get[name_of_url_variable].

For those using Webform 7.x-4.x, use [current-page:query:service_name] instead of %get[service_name].

Field display

To get the field to display to the user, so they are sure they clicked the correct row, but not be able to change it, I've set the field to 'Disabled' - just look further down the settings page - it's a checkbox.

If you want to capture information in your webform, but not display it to the user (perhaps you want to use it in an automatic email later or just need it for the back-end), then add the field as a 'Hidden' field, rather than a textfield when you're creating it.

That's it!  Let me know if you have any comments or questions.

Share

Comments

Thanks for the writeup. Very helpful.

Just a heads up for others, this blog post is in regards to Webform 7.x-3x. For those using Webform 7.x-4.x for this particular example you would use [current-page:query:service_name] instead of %get[service_name] for the default value of your Webform field.

Thanks so much, Jeremy! I know this will be useful to others.

Thanks for the heads up about Webform 7.x-4.x, was looking for that! :)

You must install token module to make it work

Thanks for the tip, that did the trick with 'Webform 7.x-4.x'! Cheers! :)

Thank you very much both of you Boriana Ditcheva and Jeremy. This is very helpful to me in my project.

Thanks a lot - that's exact what i was looking for! :)

Awesome.  I love hearing that! :)

Thank you so much! This is exactly what I was looking for. Extra thanks to Jeremy for pointing out the way it works in webform 4.x

This is Dan at UNC-CH SOG, thanks for the idea, used it to help the EFC track their resource downloads.

See you on LinkedIn.

So glad it was helpful, Dan!  You'll be surprised to know I worked at the SOG for several years some years ago.  Say 'hi' to everyone from me! :)

Just curious Boriana, have you ever used this approach to combine two or more different web forms? We have a use case where one person fills out a web form and other people need to add supplemental data, so I am looking for a way to tie the different web forms (actually their submissions) together.

Super helpful to find this after a surprising lack on info on the subject. Thanks!

hey thank you so much for this, but it is not working for me for some reason, i do pass information from availability calendar but i cannot seem to get any of it. can you help me? thanks

Mariane,

It's really hard to know what the issue may be without knowing more of your set up.  I'd say you have to make absolutely sure that the format of the URL is exactly right and that your variable names match perfectly.  There are a few areas where disconnects like that may happen...

In webform 4 it is changed a little. You use [current-page:query:service_name] instead of %get[service_name]

Nice post, thanks. I'm trying to pass the replacement pattern [current-page:title] to the webform field, so I can track which page the user clicked on to submit to the form...it is passing the pattern, but not the actual page title. Any suggestions?

I'm not using a view - but simply putting this URL on a page: /content/test-form?page_name=[current-page:title]. I have configured the test-form (Webform) to place the value from this link into the field page_name. It works, but what I'm not seeing is the page title, just the [current-page:title] code. Do I need to use a view to make this work?

Seems like webform 4 isn't supporting those %get variables and if you have webform 4 installed you can't downgrade to webform 3 unless you have some db backup before running update.php.
more info on: https://www.drupal.org/node/1609324

has someone found a way to get those variables on webform 4?

Thanks for this article!

This is awesome and gets me halfway to where I want my form to go. How can I use this to securely tell the form what email to send to? I'm not interested in passing the email in the URL or a hidden field as I'm trying to keep those addresses "secure." Since I'm passing the name of the taxonomy term, how can I tell it to use the email from that term?

Thanks a lot,[current-page:query:service_name] working for me

Awesome tutorial, got it working (webform 4) to fill in a text input field but how would I get it to select an option in a dropdown (select options field)?
Can't set a default for Listbox?

John
suncorjohn@gmail.com

Thanks a lot for your article. You helped me a lot! :)

This was a really excellent, helpful article... once I read the comments. Please please please, add the info about 7.x-4.x to the main content. Thanks so much for posting!!!

Done!

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.