3

I was wondering how to add more fields to a form after submitting that form. So the form should have these fields for example:

  • Name (textfield)
  • City (select list)
  • Find (button)

and then when user performs the submission, the result page should look like this:

  • Name (textfield)
  • City (select list)
  • Gender (radio buttons) <-- so they can refine the search by gender
  • Find (button)

How do I achieve that?

3 Answers 3

2

You need to add a submit handler to the button that updates some specific variable in $form_state so you can determine the "step" of the form. Plus, a $form_state['rebuild'] = TRUE; to rebuild the form.

Assuming you are using Drupal 7, a hook_menu or hook_block_view (or any other function) is calling drupal_get_form function with "my_form" as argument.

function my_form(&$form, &$form_state) {
  $form['name'] = array(
    '#type' => 'textfield', 
    '#title' => t('Name'),
  );
  $form['city'] = array(
    '#type' => 'select', 
    '#title' => t('City'),
    '#options' => array(
      1 => t('Kandy'), 2 => t('Anuradhapura'),
    ),
  );
  if (isset($form_state['next_step']) && $form_state['next_step']) {
    $form['gender'] = array(
      '#type' => 'select',
      '#title' => t('Gender'),
      '#options' => array(
        'm' => t('Male'), 'f' => t('Female'),
      ),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Find'),
  );
  return $form;
}

function my_form_submit($form, &$form_state) {
  $form_state['next_step'] = TRUE; // You can also change this to FALSE in validate function to prevent the gender from appearing. 
  $form_state['rebuild'] = TRUE; // Don't forget this!

  // Do other stuff here.
}

Note how I conditionally added the gender form element, and how the submit handler sets that value.

When the form is loaded for the first time (with empty $form_state), the condition to add the gender element will be FALSE, so no element added. Then, the form submit handler adds the values so in the next form rebuild, element gets included. Tricky part is the $form_state['rebuild'] = TRUE; part.

Good luck!

1

You would want to do some kind of multi-step form. This is generally not a trivial task, but it can be done. There are a quite a few tutorials around on how to do it, but you should look at the official Docs for it first here.

Another good resource for multi-step is the "Form Wizard" component of the CTools suite, which can be found here.

Hope this is helpful. One of my upcoming projects concerns this, so I am trying to learn all I can about it as well.

0

This seems to be proper place to use AHAH framework. Here and here you have doc about it.

2
  • Thank you but I guess AHAH is useful when you want to add elements dynamically without loading the page (more like Ajax stuff)? if I am right, I need to add the element after the page is reloaded and form has been submitted
    – Luca
    Commented Sep 4, 2011 at 16:44
  • AJAX or #states can do the hide/show stuff in the forms but this is more of a multistep form.
    – AKS
    Commented Jan 21, 2013 at 1:18

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.