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.
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.
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].
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.