How to create dynamic select list in form using FAPI I am using Drupal 7. I have created two form (FAPI) fields of type select , State and District.
The district field should contain the dynamic options based on the state selected.

State field:

 $form['state'] = array(
  '#title' =&gt; t('Name of your State:') ,
  '#type' =&gt; 'select',
  '#default_value' =&gt; array(
  ),
  '#options' =&gt; $options,
  '#required' =&gt; '1',
  );


District field: 

 $form['district'] = array(
  '#title' =&gt; t('Name of your District:') ,
  '#type' =&gt; 'select',
  '#default_value' =&gt; array(
  ),
  '#options' =&gt; district_list($form_state['values']['state']), // district_list is the function which will return the list of district based on name of state passed.
  '#required' =&gt; '1',
  );


The above code doesn't work, is there anything I am missing ?
You need to use the #ajax property in your State select list, with a callback function that returns the district element.  So something like this:

$form['state'] = array(
  '#title' =&gt; t('Name of your State:') ,
  '#type' =&gt; 'select',
  '#default_value' =&gt; array(
   ),
  '#options' =&gt; $options,
  '#required' =&gt; '1',
  '#ajax' =&gt; array(
    'callback' =&gt; 'MY_MODULE_state_ajax_callback',
    'wrapper' =&gt; 'edit-district',
 );


then create a callback called MY_MODULE_state_ajax_callback($form, &amp;$form_state)
and within the function, 

MY_MODULE_state_ajax_callback($form, &amp;$form_state) {
    $form_state['rebuild'] = TRUE;
    return $form['district'];
}


It may be slightly more complicated than this, based on your use-case, but this is the meat of it.