1

I have two pages, on the first page, the user types in an ID. Using hook_form_alter, I then override the submit function and add my own functionality that will then grab information from a database about the id, such as their name and email.

It then directs the user to a second page with empty fields asking for name, email etc. My question is, what is the easiest way I auto-fill these fields with the data fetched previously?

thanks!

1

2 Answers 2

0

As mentioned by @abhiklpm using $_SESSION and '#default_value' from FAPI can do the trick.

Something like, after first form submission grab values you want to autofill and put them into current session simply by doing. Assuming 'first_name' and 'last_name' are form fields.

   $values['first_name'] = 'test';
   $values['last_name'] = 'test2';
   $_SESSION['module_name']['my_multistep_form'] = $values

Alter the second form to add '#default_value' property to form elements.

 $form['first_name']['#default_value'] = $_SESSION['module_name']['my_multistep_form']['first_name'];
 $form['last_name']['#default_value'] = $_SESSION['module_name']['my_multistep_form']['last_name'];

Remember to unset values from $_SESSION in second form submit handler, use

unset($_SESSION['module_name']); to unset the session.

You might want to have a look on ctools multistep forms as well http://internetdevels.com/blog/ctools-multistep-wizard-example .

1
  • #default_value seems to be what I'm looking for. Whereabouts would I insert the code to modify that value? sorry for the dumb question, just started with Drupal Commented Jan 12, 2015 at 19:57
0

You should check out https://drupal.stackexchange.com/a/54387/4387

function MYMODULE_some_form($form, &$form_state) {
  // If the form has been submitted, output something related to the submission
  if (!empty($form_state['values']['some_field'])) {
    // Get data from database and show second step fields.
    $form['user_name'] = array(
      '#type' => 'textfield',
      // ...
    );

  }
  else {
    // Otherwise build the normal form (first step).
    $form['user_id'] = array(
      '#type' => 'textfield',
      // ...
    );
  }

  return $form;
}

function MYMODULE_some_form_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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