Last updated October 18, 2014. Created on August 17, 2008.
Edited by matthemattical, sunshinee, fizk, cluther. Log in to edit this page.

Webform allows several special values to be used as defaults when configuring a component. The special placeholders are commonly called tokens. The tokens Webform supports include:

%username
%useremail
%site
%date
%server[key]
%session[key]
%get[key]
%post[key]
%request[key]
%profile[key]

Webform 7.x-4.x and later branches use the Token module.

Webform 7.x-3.x and prior branches do not use the Token module for its replacements, so Webform's tokens may vary from tokens used in other modules. However, you can use the Webform Patched module to use Token module tokens.

Sections

Populating a Field with User Data

Webform includes several tokens that contain user data such as %username and %useremail. In addition, Webform supports any field added through the core profile module. For example, if you had profile fields setup for "First Name" and "Last Name" you could set these values to be the default values for a Webform component.

When setting up the profile fields at Administer->User management->Profiles (admin/user/profile), take care to remember the "Form name" value, as this will be needed when configuring the default value of Webform components that will contain user information.

Configuring a profile fieldSetting up a profile field at admin/user/profile/add/textfield. Note the "Form name" value is set to "profile_first_name"

After checking the configuration of a profile field, you can use the profile Form name as the default value in a Webform component.

Configuring a Webform componentWhen configuring a Webform component, use the Profile Form name in the special %profile[form_name] token.

If the current user has filled out the First name profile field, their name will automatically be used as the default value when filling out the webform.

Setting Default Values from the URL (7.x-3 and earlier)

The %get[key] supported by Webform allows form values to be populated from the URL.

Configuring a Webform componentSetting the default values of a component to use a %get[key] value.

The default value populated from the URLThe default value of the field populated from the URL.

Setting Default Values from the URL (7.x-4)

To set the default values of a component to use either:

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

t-wheeler’s picture

Seems to work with both 5.x and 6.x versions. After submitting a comment, an 'anonymous' visitor will have these values stored on their computer which can be used as a token value:

%cookie[comment_info_name]
%cookie[comment_info_mail]
%cookie[comment_info_homepage]

This might be useful only in limited situations. I couldn't easily locate this information so posting it here in case it is of interest.

Todd Wheeler
Drupal Web Developer
http://todd-wheeler.net

sreyas’s picture

Hi

I am trying to retrieve date from the webform url.

This is my url http://domain.com/booking-form&day=2011-02-28&slot=3

I need to get the date on webform as the default value. So I put in %get[day] as default value for date field. But this is giving an error
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct()

When i tried printing $_GET['day'] it correctly gives me 2011-02-28 which is the expected format for date field in webform.

I tried directly insert 2011-02-28 in the default value field and it works just fine.

Could you please let me know how i can retrieve the date information from url to the webform field.

------------------
Ciril Sreedhar
Sreyas IT Solutions
Server Administration | Web Designing | Web Programming

geoffs52’s picture

Ran across this at http://drupal.org/node/228622.

I noticed a lot of people are trying to do something with the values entered into webform, after it is submitted.

What I am trying to do is send values to webform, from a node. Any one have any ideas/solutions?

i have an inventory, with nodes created with CCK, and am using webform to allow people to request items in the inventory, while being able to track who it is is requesting what items.

For example if someone wanted a particular book, they could search for that particular book node, click on a link and there would be a request webform. But I want to pre populate some of the webform fields, with information from that particular node. Hope that makes sense

thanks for any help anyone might be able to provide
----------------------------

You can pass default values to a webform by setting a query string on the webform page URL. Like this:

<a href="node/10?email=nate@example.com&name=Nate">Fill out form</a>

Then in your webform configuration, set the default value for the fields that will receive these values as such:
%get[email]

There are reports of %get not working correctly in multi-page forms however, so watch out for that bug.
-----------------------------

I have been trying to do the same thing. Thanks for the info quicksketch!

For my situation I needed the default values to be based on each individual node listed on a page. One of the values was the node title, so for my link to the form I put:

<a href="/node/10?title=<?php print $title; ?>">Fill out form</a>

But I would also like to pass the node's taxonomy term to the URL for the form to get. Any idea if this is this possible?
-----------------------------

Eh, taxonomy is one of the worst data structures in all of Drupal. You can pull it out be it's a pain. After doing a print_r() on a $node object, you can see the taxonomy structure. It'll end up being something like this:

<?php
$tids
= array_keys($node->taxonomy);
$tid = array_shift($tids);
?>

<a href="/node/10?title=<?php print $title; ?>&term=<?php print $tid ?>">Fill out form</a>

You could shorten it down with:
<a href="/node/10?title=<?php print $title; ?>&term=<?php print array_shift(array_keys($node->taxonomy)); ?>">Fill out form</a>

And that'll get you the first term for that node. If you use multiple terms or vocabs though, you'll need to loop through all the terms to find the right vocabulary you want.
-----------------------------

This was a few months ago, but to provide an update on what I ended up doing as a workaround to get the taxonomy term and pass it to a webform field as the default value...

Since I use Pathauto to create URL aliases, I set this particular content type to have URLs in the format /taxonomy-term/node-title/node-id. By then passing the URL to the webform, the taxonomy term I needed would be available to me.

Because I wanted a link to the webform on every page for this content type I added this link to my node-contenttype.tpl.php file:

<a href="/my-webform?field1=<?php print $title; ?>&field2=<?php print $node_url; ?>">Go to my webform</a>

(Change my-webform, field1 and field2 to suit your purposes.)

This then passes the node title and URL to the webform. I get it by using the default values of %get[field1]
and %get[field2] in two seperate fields. (As a side note, field2 - the URL - is hidden to the user as it's for admin purposes only - very handy.)

Hope that might help someone down the line!
------------------------------

awm6392’s picture

I have an issue with the %get[] method described here. I have a site that uses a webform to order a number of products. The same webform is embedded in a number of product pages and there are hidden fields that use the %get[] method to call variables from the URL so the order email can say what product is being ordered and so on. And this works fine.

HOWEVER my problem is: what if someone were to get to the product order page through some other means than the link provided on each product page which passes the variables to the URL? For example, instead of going through the product page, a customer already knows what product they want and the URL of the page so they simply type it into the browser. Page shows up, but the variables will not be passed through the URL so when they fill out the form, it will be impossible to tell which product they want.

Is there a way around this, such as passing variables to a webform from within the same node, or passing to the URL from the same page? Everything that I've seen so far has just talked about the %get[] method.

Cheers

Alex

carlchen414’s picture

Hi, I'm seeking this way as well. Have you fixed it out? If yes, plz would you give me a shot how to do this. Your guidance will be great. Thanks in advanced.

regards

treyc’s picture

I hope I'm posting this in the right area. I also hope I haven't overlooked the topic somewhere else for that matter. If so, please accept my apologies and please point me in the correct direction for the answer.

I would like to have my webforms pre-populated with the information entered in a users profile for the UberCart Addresses module. I don't know how to write PHP, so altering the code myself is not an option.

I appreciate any constructive assistance.

Thanks!

Trey

bboybenny’s picture

I am looking for the exact solution to your problem, I wonder did you solve it yet? If so please help me out. Thanks.

chinita7’s picture

I'm trying to do the same thing as you have described. But it doesn't work properly. I also have an inventory with nodes created with CCK and need send the value of 'node title' and 'node number' to webform. So I made a link like this.

<a href="/mywebform/?title=<?php print $title; ?>&nid=<?php print $nid; ?>">Fill out form</a>

But it shows only NOT FOUND page with URL something like
/mywebform/?title=<?php%20print%20$title;%20?>&nid=<?php%20print%20$nid;%20?>

Do you have any idea why this is not working?

Useful Idiot’s picture

Were you able to accomplish passing the taxonomy to the form? I am having a very tough time with this, any chance you could help a guy out? Or point me in the right direction?

GoalGorilla’s picture

Useful default value for referer page to the webform: %server[HTTP_REFERER]

perandre’s picture

To get the url the user is on when he/she submits the form, simple make a hidden field with default value %server[REQUEST_URI]. Works as a charm!

Per André Rønsen | Front | Twitter: @perandre

quicksketch’s picture

It came to our attention that %server (and %cookie and %session) variables were potentially dangerous when they were cached by the Drupal anonymous page cache, so using %server variables will not work for anonymous users. However you can still use %get, making %get[q] a valid substitute for REQUEST_URI, only it will be the internal Drupal path. You can then run this through the url() function to generate a full URL.

EDIT: This is now back again in Webform 3.15 and higher through use of the new "secure" option on hidden components. This option is selected by default.

perandre’s picture

Thanks for your input. Using %get[q] however, displays the link to the webform node id, not the actual page you are on (view, panel, parent node, etc).

Per André Rønsen | Front | Twitter: @perandre

Hadi Farnoud’s picture

I have exact same problem! %server[REQUEST_URI] is not working for anonymous users. is there any way around this?

gstout’s picture

I had already finished a whole set a vars that get loaded into the session when I found out you can use sessions for anon form submissions. To me this seems crippling.

I don't see how you can run the url() function on %get[q] when that token is only available in the webform fields and those don't support php? Does anyone have a work around for this?

(also this new limitation should be stated somewhere in this paragraph in the token values.

"You can use %server[key] to add any of the special PHP $_SERVER variables, %session[key] to add any of the special PHP $_SESSION variables and %get[key] to create prefilled forms from the URL. %cookie, %request and %post also work with their respective PHP variables. For example %server[HTTP_USER_AGENT], %session[id], or %get[q]."

)

--
Greg

Enno’s picture

I suppose you wanted to say

"I found out you canNOT use sessions for anon form submissions"

. At least I found it to be like that.

My problem: Web form added as view block to a node. The webform submission needs to reference the node that it is called from. User will be anonymous.
The token %get[q] ist not helping as it gives the url of the webform and not of the node.
The token %session[] cannot be used due to anonymous user.

My solution: Setting the node url per javascript as described in http://drupal.org/node/342183#comment-2105788

Simply add the javascript inline or as a separate file using

<?php
  drupal_add_js
(path_to_theme() .'/scripts/SCRIPT_NAME.js');
?>

in webform-form.tpl.php. Then use jquery to fill the value of the url using for example:

$(document).ready(function(){

        var url = window.location.pathname;
      
        $("#edit-submitted-<name>").val(url);

    });

grahambeek’s picture

I've used $_get to specify an ID which is passed in a webform url.

What I'd like to do is run a database query using this ID to populate the default values e.g. name, address, age etc. I probably need to put in a hook somewhere, but I don't know where. I did try and put it in the body, but it didn't work. It seemed like the fields were already populated by the time the query was run.

Any pointers would be greatly appreciated!

felipe’s picture

I have the same need, to prepopulate the webform with data from another table. The aim is to allow customers (anonymous to drupal) to update their customer data, keeping their address and phone updated without needing to call a customer service attendent.
The customer data table is already in the drupal database in a separate table.
Is there any means of integrate this with webform? So the webform fields can come prefilled based on an ID passed through the url?

oskylark’s picture

In a custom module add a hook_form_FORM_ID_alter function e.g. for D6:

<?php
function MYMODULENAME_form_FORM_ID_alter (&$form, &$form_state) {
 
// go get some data based on $_GET['ID']
  // then set a field's default value
 
$form['submitted']['FIELD_KEY']['#default_value'] = $data_result;
}
?>

FORM_ID will be webform_client_form_NID (look for the form_id hidden input value in the form's HTML).
FIELD_KEY will be that defined for the component you're setting the default value for.

See also Programmatically modifying a webform with hook_form_alter() in this guide.

luco’s picture

I needed to have a drpodown pre-selected.

let's say you have a dropdown with pairs of keys and values like

key1|value1
key2|value2
key3|value3
key4|value4

my mistake was to set its default value to

%get[foo]

and then in the URL,

example.com/contact?foo=value1

which didn't show nothing.

actually you have to specify

example.com/contact?foo=key1

in the address bar. and voilà ;]

_________________________

"There is no off position on the genius switch."
- David Letterman

Diegen’s picture

How do you spesify a variable like that in the address bar ? Do you simply use URLaliases ? Or can a hidden form work too ?

UNarmed’s picture

What would i type if i want to pass more than one value? e.g i want to pass both of the below values?

name=[name]
title=[title]

Figured it out, you need to seperate them with a ampersand "&"

pmelab’s picture

I have the problem that default values get lost if the user doesn't fill out the form correctly. Is this intended? Or a bug?

makt’s picture

I have products with "request to purchase" links to a webform. I want to pass the product name as a default in the webform. I tried following the instructions above and used the following for the link::

<a href="/link?title=<?php print $title; ?>">Request to Buy</a>

and

%get[title] as the default value on the webform field.

BUT nothing is showing up in that field when I use the php. If i put static text to replace

<?php
 
print $title;
?>

it shows up in the field. Is there something else I can use to automatically populate the title field in the webform?

Matt B’s picture

I've created a select list of countries as per http://drupal.org/node/221490, and I have the same list without the keys as a field set in the Profile. Profile does not allow the use of safe keys. This means I cannot use %profile[profile_country] as a default value, unless there is a way for the default to be set based on the value, not the key in the webform select list. Has anyone else solved a similar problem?

sorensong’s picture

Is there a way to use the subdomain as a default value?

Anandyrh’s picture

How can I pass multiple tokens on the URL and print on multiple fields?

Thanks & Regards,
AnAnD | Drupal | Photographer @ MCS | Facebook | Twitter

henfiber’s picture

I'm trying to set a default value for a hidden field capturing a POST Array Variable (by the way, webform form keys are arrays) but the literal is not replaced with the posted value.

Should I escape/encode the field key in %post[key] when the variable is an array?

e.g
%post[submitted[participant_details][contact_details][name]]

This does not work.
I also tried url-encoding the brackets inside the key like this:

%post[submitted%5Bparticipant_details%5D%5Bcontact_details%5D%5Bname%5D]

This does not work either.
Instead, adding %post[form_id] (form_id:non-Array form variable) in the same field works as described.

henfiber’s picture

Finally Figured this out
For everyone interested there is a recent patch: http://drupal.org/node/824606#comment-5507302

szantog’s picture

A little helper to prepopulate webforms from a custom block: http://drupal.org/project/webform_prepopulate_block

AdamMMA’s picture

How would you be able to prevent cross site scripting from occurring when using default values from a URL? For example, how could you stop a user adding a script as below:

<a href="/node/1?&param1=blahblah"><script>alert(123)</script>blah&param2=blahblah">Link</a>
DrupalDan’s picture

It seems that D7 supports adding additional user fields at admin/config/people/accounts/fields, and I don't any find "profile" fields. I'm using D7 and I created a field for First Name at admin/config/people/accounts/fields. The machine name is "field_first_name", the field type is "text", and the widget "text field". Can anyone please tell me the correct way to use token associated with a field like this?

Thanks!

play4quarters’s picture

I've got the same question...using the exact same setup as your describe.

Thanks, in advance, for anyone's help on this.

leanne9’s picture

Also having this problem, using the Profile2 module I cannot add %profile information, seems a very popular issue-- http://drupal.org/node/1398542 and http://drupal.org/node/1857038 as well-- any help would be widely appreciated!

Update: I see that using Webform 4.x enables use of the [user:xxx} tokens... which are not used in 3.x http://drupal.org/node/1609324#tokens ...I am hesitant to update to another development module but might have to!

cmejo’s picture

For D7 you just write %profile[field_xyz]. E.g., if you have a field named field_first_name on the admin/config/people/accounts/fields page, you'd write %profile[field_first_name] into the webform value field. Does that solve your problem?

Drupal Architect and CEO, Cronode
www.cronode.com

leanne9’s picture

Thanks for the help, but no, unfortunately that didn't work... it just shows up blank, even though I have content entered in that field. I tried other account levels too, thinking it was maybe because I was an admin, but it didn't work for any other roles either. If I enter %profile[field-first-name] with dashes instead of underscores, the default value just prints as %profile[field-first-name]

froboy’s picture

There's a discussion of this problem at http://drupal.org/node/1023918 with some patches, but the tl;dr is "Use Webform 4.x to get full token support in D7".

VincenzoL’s picture

i used webform 7.x-3.20 for a wizard wiht some step.
After the first step i put new forms with the default value kept in "INCLUDED E-MAIL VALUES".
Individual values may still be printed if explicitly specified as a %email[key].

darksnow’s picture

Hi, I'm using 7.x-4 and I'd like to set the default value from a GET parameter is it's set.

The instrcutions above work perfectly, thanks for that.

But, what if the value is not set? I'd like the select list to default to the first entry rather than saying '- Select -'.

Any ideas how to achieve this?

Web Developer at http://www.darksnow.net
Web Master of the UK Snowboarding community at http://www.darksnow.co.uk

web506’s picture

Hey Darksnow,

Did you find the solution to your issue? seems like I want to do the same thing. Please let me know, thank you!

Gonzalo Garcia
Freelance Webmaster

cesoftvn’s picture

Webform 4.x

Get default value [current-page:query:?]

?foo=bar

then [current-page:query:foo]

web506’s picture

Is there a way to set the value of a webform field IF [current-page:query:foo] comes empty? I have been trying to get it to work with rules and can't seem to find the solution, and in Rules Data fields are very confusing too.

IF [current-page:query:foo] is Empty THEN set the X default value to Hidden Field.

Thanks very much for any help in advance!

Gonzalo Garcia
Freelance Webmaster

smurfxx’s picture

Is there a working solution? I have the same problem...

tthenne’s picture

I'm also looking for a solution to replace the token if the query parameter is not present. As an example if I have a textfield set to default value: [current-page:query:subject] and the query string in the url does not have ?subject= (http://example.com?body=text) than the value for the [current-page:query:subject] field would be blank. Instead it is showing: [current-page:query:subject] which is not user friendly.

dave-at-sherpa’s picture

It took me longer than I expected to locate someone with the same question, which makes me wonder if this is a new bug. I've been using Webform for a while now, and haven't experienced this before. So either I did something stupid along the way, or this is a new issue.

I'm using tokens to fill in basic profile info in a webform (4.7). For any registered user, it works as expected. The webform fields all display the info called by the tokens (first name, last name, email, etc.). But for anonymous users, the form shows the actual tokens, e.g., [current-user:field_first_name], [current-user:field_last_name], etc.

Any advice on this out there?

tthenne’s picture

The only way I've been able to solve this so far is to use javascript (jquery) to check the input value and clear the field if it equals the token value. I did open a support ticket here: https://www.drupal.org/node/2476685 in case you want to subscribe to that.