2

I need to get the submitted values of a multistep webform. It looks like webform doesn't save values from earlier steps in the usual place: $form_state['values']['submitted'] or $form_state['input']['submitted']. I just need the submitted values of every form component at the last page.

I actually made a custom submit handler to store the values from each form in my own location in $form_state, but I was wondering if there was a better way.

4

You will find the multi-page values stored in

$form_state['storage']['submitted']

Note that this array does not contain values from the current page and will be empty if the current page is 1.

And incidentally you can find out which page was just submitted by checking

$form_state['storage']['page_num']
0

My first guess would be you, if you haven't already, is to integrate the form with AJAX, so when you Click Submit to continue to the next page, it will also trigger an AJAX event, which still store the field variable information based of your custom Submit Handler function.

Here is a snippet from the Advanced Form Block Module, which provides AJAX for Node creation.

$form['#after_build'][] = 'afb_after_build';
    $rand = rand();
    $form['actions']['submit'] = FALSE;
    $form['actions']['preview'] = FALSE;
    $form['actions']['delete'] = FALSE;
    $form['changed']['#default_value'] = time() + 300;
    $form['#id'] = !empty($form['#node']->form_id) ? $form['#node']->form_id : $form['#id'];
    $form['ajax-submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save Node'),
      '#weight' => 50,
      '#id' => 'ajax-submit-random-' . $rand,
      '#ajax' => array(
        'wrapper' => !empty($form['#node']->form_id) ? $form['#node']->form_id : $form['#id'],
        'callback' => 'afb_ajax_handler',
        'effect' => 'fade',
      ),
      '#submit' =>
      array(
        1 => 'node_form_submit',
      ),
    );

It calls a function to Save the fields, without refreshing the page...

/**
 * Ajax Handler that returns the forms in each individual blocks.
 */
function afb_ajax_handler($form, $form_state) {
  if (isset($form['#node']->nid)) {
    $id = $form['#id'];
    module_load_include('inc', 'node', 'node.pages');
    $node = node_load($form['#node']->nid);
    $form_state['build_info']['args'][0]->form_id = $id;
    $form = drupal_rebuild_form($node->type . '_node_form', $form_state, $form);
    $form['#id'] = $id;
    return $form;
  }
  else {
    $node = $form_state['node'];
    if (!empty($node->nid)) {
      $id = $form['#id'];
      $new_node = (object) array(
        'uid' => $node->uid,
        'name' => $node->name,
        'type' => $node->type,
        'language' => $node->language,
        'ajax_form' => 1,
        'delta' => $node->delta,
        'form_id' => $id,
      );
      $new_form_state = array();
      $new_form_state['build_info']['args'] = array($new_node);
      $new_form_state['values'] = array();
      $new_form_state['method'] = $form_state['method'];
      form_load_include($new_form_state, 'inc', 'node', 'node.pages');

      $form = drupal_rebuild_form($node->type . '_node_form', $new_form_state);
      $form['#id'] = $id;
      return $form;
    }
    else {
      return $form;
    }
  }
}

That being said, that is not a direct answer to your question, because without knowing the fields you are storing I can't really help you write a module, but this should give you a basic example of what "should" be done.

I think you might be able to get away without worrying about a wrapper, because you are storing data, but I could be wrong on this.

-2

I doubt that this is the correct way to do it but... I was able to pull values from the previous page using the following,

$_POST['submitted']['FIELD_ID']

where FIELD_ID is the ID of the desired form field.

This seems rather sloppy and I would like to find a better solution.

EDIT: Coleman's solution is the correct solution and should be used instead of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.