Set the value of a hidden form component I have a Webform in drupal 7. I want to add a hidden field porgramatically to it, and I would like to set the value of this field per form.
I want to achieve all these in a custom submit function, as I want to use the sid (serial ID) of the Submission, to generate a unique ID for each Submission.
At the moment everything is working, except to set the value.
I googled it a lot, tried out hundreds of codes but non of them worked in my case.

Here is my code:

&lt;?php
$form['#submit'][] = 'webform_custom_submit';

function webform_custom_submit($form, &amp;$form_state) {
  if ( !array_key_exists('my_uniq_id', $form['#node']-&gt;webform['cids'] ) ){
    $components = array(
      0 =&gt; array(
        'name' =&gt; 'My Unique ID',
        'form_key' =&gt; 'my_unique_id',
        'type' =&gt; 'hidden',
        'weight' =&gt; 1,
        'pid' =&gt; 0
      )
    );
    webform_ensure_record($form['#node']);
    foreach ($components as $component) {
      $component['nid'] = $form['#node']-&gt;nid;
      $form['#node']-&gt;webform['components'][] = $component;
      webform_component_insert($component);
    }
  }
  $my_uniqe_key = generate_some_unique_key_function();
  //Here I wan't to set the value of the component 'my unique ID', but nothing worked yet
  $form_state['my_unique_id']['#value'] = $my_uniqe_key;
}


Thanks for the help, I'm sure I could find the answer somewhere, but I tried it 3hours long... 

Here  is the $form and $form_state written out with dpm from my webform_custom_submit function.

And here are some codes, I tried already:

&lt;?php
$form['values']['my_unique_id'] = $my_uniqe_key;
$form['values']['my_unique_id']['#value'] = $my_uniqe_key;
$form['values']['my_unique_id']['value'] = $my_uniqe_key;
$form['submitted']['my_unique_id']['#value'] = $my_uniqe_key;
$form['my_unique_id']['und']['0']['value']['#value'] = $my_uniqe_key;
$form_state['values']['my_unique_id'] = $my_uniqe_key;
$form_state['values']['my_unique_id']['#value'] = $my_uniqe_key;
$form_state['values']['my_unique_id']['value'] = $my_uniqe_key;
$form_state['my_unique_id']['und']['0']['value']['#value'] = $my_uniqe_key;
$form_state['values']['my_unique_id'][LANGUAGE_NONE][0]['value'] = $my_uniqe_key;
drupal_array_set_nested_value($form, array('submitted', 'my_unique_id', '#value'), $my_uniqe_key;);
form_set_value( array('submitted','my_unique_id'), array(0 =&gt; array('value' =&gt; $my_uniqe_key;)), $form_state) ;

$cid = $form['#node']-&gt;webform['cids']['my_unique_id'];
$form_state['values']['submitted'][$cid] = "static text";
You should use the presave hook provided by webform instead of adding additional submit handlers to the form.

/**
 * Modify a Webform submission, prior to saving it in the database.
 *
 * @param $node
 *   The Webform node on which this submission was made.
 * @param $submission
 *   The Webform submission that is about to be saved to the database.
 */
function hook_webform_submission_presave($node, &amp;$submission) {
  // Update some component's value before it is saved.
  $component_id = 4;
  $submission-&gt;data[$component_id][0] = 'foo';
}


This is a lot cleaner than adding custom submit handlers to the form (your custom handler is called after the webform submission handler, which can be a big part of the problem).

You have access to the node object in the presave hook so additional checks can be added beforre altering the component.