Last updated June 13, 2011. Created by quicksketch on January 23, 2009.
Edited by rares, Durrok, webchick. Log in to edit this page.

This is a collection of PHP snippets that you can use to add custom submission handling to your Webforms. Using submission code, you can change any of the $form_values or $form_state variables that are available to you, and these values will be saved with the changed values in the database.

Please note: Webform version 3 no longer supports additional validation and additional processing snippets. For more information about support for PHP code execution, see http://drupal.org/node/754580 and http://drupal.org/project/webform_php.

A strange Webform quirk: Values are split between $form_values['submitted'] and $form_values['submitted_tree']. Values set in the 'submitted' key are saved to the database, while values in the 'submitted_tree' are sent out in the sent e-mails. The submitted tree will follow the same format as your form (that is, fields will be nested), but the 'submitted' values will be a single array, keyed by the unique component ID for each field.

An example:

<?php
$component_key
= 'first_name';
$component_cid = 6;
// The First Name field is within a fieldset for "Name".
// Updating this value will set the first name to to "Bob" in the e-mail.
$form_values['submitted_tree']['name'][$component_key] = 'Bob';
// Despite the field being in a fieldset, in the submitted array,
// it is identified directly by the component CID.
// Updating this value will set the the first name to be "Joe" in the database.
$form_values['submitted'][$component_cid] = 'Joe';
?>

or if you just wanted to set the value to a php variable:

<?php
$first_name
= $form_values['submitted_tree']['first_name'];
$last_name= $form_values['submitted_tree']['last_name'];
?>

This snippets are only intended for use with the Webform 2.x module. They will not work with CCK or other modules.

To use any of these snippets:

  1. Visit the permissions page (admin/user/access in D5 or admin/user/permissions in D6) and ensure the current user has the "use PHP for additional processing" permission.
  2. Create or edit a new Webform
  3. Under the "Webform advanced settings", enter the snippet, including the <?php ?> tags.

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

Comments

hi
where to write Above Code Snippet can any one identify the File where i can write this Code Snippet
regard

Hello,

If you have the permission to add PHP code, you find an additional processing field in the advanced parameters group of the webform creation page. (I am looking at a drupal site translated into french, so the names I am using here may be only approximately right.)

Bas Michielsen

I'm trying to do a check to see if a few checkboxes are checked or not. How would you modify the code for that? I've already found how to see the different options like these:

$submission->data[17]['value'][0]
$submission->data[17]['value'][1]
$submission->data[17]['value'][2]
$submission->data[17]['value'][3]

The above just shows what the checkboxes' values are but not if they are actually checked or not. I assume I would need to change the ['value'] to something.

Thanks

-Bill