Same name and namespace in other branches
  1. 6.x-1.x form_example/form_example_tutorial.inc \form_example_tutorial_8()

Example 8: A simple multistep form with a Next and a Back button.

Handbook page: http://drupal.org/node/717750.

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

1 string reference to 'form_example_tutorial_8'
form_example_menu in form_example/form_example.module
Implements hook_menu().

File

form_example/form_example_tutorial.inc, line 385
This is the Form API Tutorial from the handbook.

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;
}