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!