user3642164
user3642164

Reputation:

Passing a variable from a form to another in Drupal

Let's say I have custom module with Drupal.
I want to let user to type what skill their have in first form and after that show their skill as a title in second form and type percentage of their skill in textfield.

function skillbar_form($form, &$form_state) {
  $form['html5'] = array(
    '#type' => 'textfield',
    '#title' => t('HTML5'),
    '#default_value' => variable_get('html5'),
    '#description' => t('Enter a percent of your HTML5 skill'),
  );
  return(system_settings_form($form));
}

snapshot

Upvotes: 1

Views: 1895

Answers (2)

Naveen Kumar
Naveen Kumar

Reputation: 989

You can pass the data from one form to another through URL in the submit form of first form.

function skillbar_form_submit($form, &$form_state){
$data_a = $form_state['values']['html5'];

 $form_state['redirect'] = array('url_page2', $data_a);
}

In the other form just retrieve the data as by passing the $data_a as argument. So $var will have the value as 'html5'.

example_form($form ,&$form_state, $var){
  ...
}

And for the URL you will have to send it in items array

  $items['url_page2/%'] = array(

  'page callback' => 'drupal_get_form',
  'page arguments' => array('page2_form', 1),
  'type' => MENU_CALLBACK,
  'access callback' => TRUE,
);

Upvotes: 0

Ajit S
Ajit S

Reputation: 1341

Multistep Form is you friend, if you are building your form using the Form API.

Otherwise there are various modules which could help you in building forms with more than one step. A couple of them being:

  1. Multi-step forms
  2. Multi-Step Registration

Note: Multistep forms generally collect all the data from various steps. The data is mostly submitted at the final step (which is also a best practice).

Upvotes: 1

Related Questions