1 form_example_tutorial.inc form_example_tutorial_8($form, &$form_state)

This is Example 8: a simple multistep form with a Next and a Back buttons.

For more extensive multistep forms, see form_example_wizard.inc

Adds logic to our form builder to give it two pages. The AJAX Example's Wizard Example gives an AJAX version of this same idea.

See also

form_example_tutorial_8_page_two()

form_example_tutorial_8_page_two_back()

form_example_tutorial_8_page_two_submit()

form_example_tutorial_8_next_submit()

form_example_tutorial.inc

Related topics

File

modules/examples/form_example/form_example_tutorial.inc, line 354
This is the form API tutorial.

Code

function form_example_tutorial_8($form, &$form_state) {

  // Display page 2 if $form_state['page_num'] == 2
  if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
    return form_example_tutorial_8_page_two($form, $form_state);
  }

  // Otherwise we build page 1.
  $form_state['page_num'] = 1;

  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('A basic multistep form (page 1)'),
  );

  $form['first'] = array(
    '#type' => 'textfield',
    '#title' => t('First name'),
    '#description' => "Please enter your first name.",
    '#size' => 20,
    '#maxlength' => 20,
    '#required' => TRUE,
    '#default_value' => !empty($form_state['values']['first']) ? $form_state['values']['first'] : '',
  );
  $form['last'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name'),
    '#default_value' => !empty($form_state['values']['last']) ? $form_state['values']['last'] : '',
  );
  $form['year_of_birth'] = array(
    '#type' => 'textfield',
    '#title' => "Year of birth",
    '#description' => 'Format is "YYYY"',
    '#default_value' => !empty($form_state['values']['year_of_birth']) ? $form_state['values']['year_of_birth'] : '',
  );
  $form['next'] = array(
    '#type' => 'submit',
    '#value' => 'Next >>',
    '#submit' => array('form_example_tutorial_8_next_submit'),
    '#validate' => array('form_example_tutorial_8_next_validate'),
  );
  return $form;
}