Hello,
I've tried the multistep form guide (http://drupal.org/node/101707 and the various other posts on the subject but I'm still stuck, badly, on this.

I think it's down to $form_values not being passed back correctly.

What I'm trying to do:
1) create a form containing a list of questions (this is ok)
2) form submits & validates, a score is calculated dependent on answers (this is ok)
2a) A save button is shown to save this score.

This is where I get stuck. ** I've tried including a second form within the submit function and that doesn't work (it just gives me back the list of questions at step 1 upon submit.)

I've tried using a multistep form
A) If I use form_values then I get this problem of the form submitting after step 1 (and not going through the second case of the form. -> http://drupal.org/node/114378 Why is $form_values not set?
(http://drupal.org/node/101707)

B) I've even tried using the _POST[op] value rather than $step in the switch statement. This correctly steps through the form but the final submit doesn't work as it fails validation.
(ideally it would validate after step 1, and at step 2a seperately. Is this possible?)

Do I have to use a multistep form (if I can get it working?) or how can I get a second form to submit correctly? (as per ** above)

Hoping someone may be able to help..

Comments

hickory’s picture

I was trying to do something similar (output a second form from the submit step of a previous, multistep form) but couldn't find a way to do it - the submit function has to return a string for the redirect, it seems.

criznach’s picture

Is this drupal 4.7 or 5? In 4 I've had good luck using a button rather than a submit for every page except the last page. This causes the form callback to be called for each page. At the top of that function, you can keep track of the page number and and display the appropriate page's form array.

http://www.trailheadinteractive.com
--- Featured Projects ---
http://www.montanakitesports.com
http://www.cmrussell.org
http://www.tdandh.com
http://www.cccsmt.org
http://www.universalsemensales.com

rp9’s picture

Hi,

I'm using drupal 5. I'll see if I can replicate what you say above.

I've also tried hand coding a simple html form instead of drupal_get_form, I can then get it to submit and goto the save function but I can't seem to get hold of the form values.

jlab’s picture

This form works....

Sorry for the huge code example I'm still trying to figure out how to upload my entire module somewhere to Drupal CVS.

/* $ID$ */


/**
* Display help and module information
* @param section which section of the site we're displaying help
* @return help text for section
*/
function testmod_help($section='') {
  $output='';

  switch($section) {
    case "admin/help#testmod":
      $output = t("Multipage Form Test");
    break;
  }

  return $output;
} // funtion testmod_help

/**
* Menu Hooks
*/
function testmod_menu() {
  $items = array();

  // Entry Form
  $items[] = array(
      'path' => 'testmod',
      'title' => t('Multipage Test Form'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('testmod_entry_form'),
      'access' => TRUE,
      'type' => MENU_CALLBACK,
  );

  return $items;
} // function testmod_menu


/**
* Valid permissions for this module
* @return array An array of valid permissions for the testmod module
*/
function testmod_perm() {
  return array('access testmod', 'administer testmod');
} // funtion testmod_perm

/**
* Generate a entry form
*/
// function testmod_entry_form($nid, $form_values = NULL) {
function testmod_entry_form($form_values = NULL) {

  // In a multistep form, drupal_get_form() will always
  // pass the incoming form values to you after any other
  // parameters that you specify manually. Do this instead
  // of looking at the incoming $_POST variable manually.
  if (!isset($form_values)) {
    $step = 1;
  }

  if($form_values['op'] == 'Back') {
    $step = $form_values['step'] - 1;
  }
  if($form_values['op'] == 'Next') {
    $step = $form_values['step'] + 1;
  }

  $form['step'] = array(
    '#type' => 'hidden',
    '#value' => t($step),
  );

  switch($step) {
    case 1: // ----------------------------------------------------------------
      // Create hidden fields
      $form['testfield3'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield3'],
      );

      
      $form['testfield4'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield4'],
      );
      
      $form['testfield5'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield5'],
      );

      $form['testfield6'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield6'],
      );
      
      $form['testfield7'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield7'],
      );

      $form['testfield8'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield8'],
      );
      
      // Form Elements
      $form['step1'] = array(
        '#type' => 'fieldset',
        '#title' => t('Step 1'),
        '#weight' => 0,
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      
      $form['step1']['testfield1'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 1',
        '#description' => 'Test field 1',
      );
      
      $form['step1']['testfield2'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 2',
        '#description' => 'Test field 2',
        '#required' => FALSE,
      );
      
      $form['step1']['back'] = array(
        '#type' => 'hidden',
        '#value' => 'Back',
      );

      $form['step1']['next'] = array(
        '#type' => 'submit',
        '#value' => 'Next',
      );
    break;
    
    case 2: // ----------------------------------------------------------------
      // Create hidden fields
      $form['testfield1'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield1'],
      );

      $form['testfield2'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield2'],
      );

      $form['testfield5'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield5'],
      );

      $form['testfield6'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield6'],
      );
      
      $form['testfield7'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield7'],
      );

      $form['testfield8'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield8'],
      );
      
      // Form Elements
      $form['step2'] = array(
        '#type' => 'fieldset',
        '#title' => t('Step 2'),
        '#weight' => 0,
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );

      $form['step2']['testfield3'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 3',
        '#description' => 'Test field 3',
        '#required' => FALSE,
      );
      
      $form['step2']['testfield4'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 4',
        '#description' => 'Test field 4',
        '#required' => FALSE,
      );
      
      $form['step2']['back'] = array(
        '#type' => 'button',
        '#value' => 'Back',
      );

      $form['step2']['next'] = array(
        '#type' => 'submit',
        '#value' => 'Next',
      );
    break;
    
    case 3: // ----------------------------------------------------------------
      // Create hidden fields
      $form['testfield1'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield1'],
      );

      $form['testfield2'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield2'],
      );

      $form['testfield3'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield3'],
      );

      $form['testfield4'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield4'],
      );
      
      $form['testfield7'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield7'],
      );

      $form['testfield8'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield8'],
      );
      
      // Form Elements
      $form['step3'] = array(
        '#type' => 'fieldset',
        '#title' => t('Step 3'),
        '#weight' => 5,
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      
      $form['step3']['testfield5'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 5',
        '#description' => 'Test field 5',
        '#required' => FALSE,
      );
      
      $form['step3']['testfield6'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 6',
        '#description' => 'Test field 6',
        '#required' => FALSE,
      );
      
      $form['step3']['back'] = array(
        '#type' => 'button',
        '#value' => 'Back',
      );

      $form['step3']['next'] = array(
        '#type' => 'submit',
        '#value' => 'Next',
      );
    break;

    case 4: // ----------------------------------------------------------------
       // Create hidden fields
      $form['testfield1'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield1'],
      );

      $form['testfield2'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield2'],
      );

      $form['testfield3'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield3'],
      );

      $form['testfield4'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield4'],
      );

      $form['testfield5'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield5'],
      );

      $form['testfield6'] = array(
        '#type' => 'hidden',
        '#value' => $form_values['testfield6'],
      );
      
      // Form values
      $form['step4'] = array(
        '#type' => 'fieldset',
        '#title' => t('Step 4'),
        '#weight' => 0,
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );

      $form['step4']['testfield7'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 7',
        '#description' => 'Test field 7',
        '#required' => FALSE,
      );

      $form['step4']['testfield8'] = array(
        '#type' => 'textfield',
        '#title' => 'Test field 8',
        '#description' => 'Test field 8',
        '#required' => FALSE,
      );
      
      $form['step4']['back'] = array(
        '#type' => 'button',
        '#value' => 'Back',
      );
      
      $form['step4']['finish'] = array(
        '#type' => 'submit',
        '#value' => 'Finish',
      );
    break;
  }
  
  // This part is important!
  $form['#multistep'] = TRUE;
  $form['#redirect'] = FALSE;
  $form['#base'] = 'testmod_entry_form';

  return $form;
} // function testmod_entryform

function testmod_entry_form_validate($form_id, $form_values) {
  //echo "I'm here !!!!";
  //echo '<pre>' . print_r($form_values['step']) . '</pre>';
  $errors = array();
  
  if($form_values['op'] != 'Back') {
    if($form_values['step'] == '1') {
      if($form_values['testfield1'] == '') {
        $errors['testfield1'] = t('You must enter a value for test field 1');
      }
      if($form_values['testfield2'] == '') {
        $errors['testfield2'] = t('You must enter a value for test field 2');
      }
    } //-----------------------------------------------------------------------

    if($form_values['step'] == '2') {
      if($form_values['testfield3'] == '') {
        $errors['testfield3'] = t('You must enter a value for test field 3');
      }
      if($form_values['testfield4'] == '') {
        $errors['testfield4'] = t('You must enter a value for test field 4');
      }
    } //-----------------------------------------------------------------------

    if($form_values['step'] == '3') {
      if($form_values['testfield5'] == '') {
        $errors['testfield5'] = t('You must enter a value for test field 5');
      }
      if($form_values['testfield6'] == '') {
        $errors['testfield6'] = t('You must enter a value for test field 6');
      }
    } //-----------------------------------------------------------------------

    if($form_values['step'] == '4') {
      if($form_values['testfield7'] == '') {
        $errors['testfield7'] = t('You must enter a value for test field 7');
      }
      if($form_values['testfield7'] == '') {
        $errors['testfield6'] = t('You must enter a value for test field 7');
      }
    } //-----------------------------------------------------------------------
  }
  
  // Loop Through each error element and display error message on the screen.
  foreach ($errors as $name => $message) {
    form_set_error($name, $message);
  }
}

/**
* Submit the data.
*/
function testmod_entry_form_submit($form_id, $form_values) {
  $final_step = 4;
  
  if ($form_values['step'] == $final_step) {
    drupal_set_message('Submited');
  }
} // function testmod_entry_form_submit

Artificial Intelligence is no match against Natural Stupidity

cpill’s picture

This doesn't work. Still no data passed to the form after the first submit :(

Has anyone found a solution to this?

surfer_1’s picture

I m a new drupal user.
I want be create a multistep form.
But I don't know where do I write theese codes.
In node.module or another in the file.
Thank you.
Surfer_1

criznach’s picture

You'll want to create your own module. And I'd strongly recommend starting a new thread because this post is buried at the bottom of a long, old post.

http://www.trailheadinteractive.com
--- Featured Projects ---
http://www.montanakitesports.com
http://www.cmrussell.org
http://www.tdandh.com
http://www.cccsmt.org
http://www.universalsemensales.com

tchivoan1972’s picture

I copy and pasted this code to create a module, I have no idea how to access this form to see how it works once I enable the module lol
i tried type mydomain.com/testmod but get page 404 error