Add options to dropdown with ajax I am trying to use ajax in my form to dynamically add options to the dropdown, with the click of a button. The value entered in the textbox must be added to the dropdown. However i am not sure how to retain the added values in the dropdown.
Currently only the recently added value remains in the dropdown.

This is my module code:

&lt;?php
function dropdown_menu(){
    $items['dropdown_form'] = array(
        'title' =&gt; 'Dynamic Dropdown',
        'description' =&gt; 'adding more textfields',
         'access callback' =&gt; 'dropdown_access',
         'access arguments' =&gt; array(1),
        'page callback' =&gt; 'drupal_get_form', //used during form api
        'page arguments' =&gt; array('dropdown'),
    );
    return $items;
} 

function dropdown_access() {
    return TRUE;
} 

function dropdown($form, &amp;$form_state, $no_js_use = FALSE) {
   $form['field_name'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Field Name:  String, Woodwind, Brass, Percussion'),
      '#required' =&gt; TRUE, 
    );    

   $field_val = isset($form_state['values']['field_name']) ? $form_state['values']['field_name'] : '';

  $form['add_to_dropdown'] = array(
      '#type' =&gt; 'button',
      '#value' =&gt; 'Add To Dropdown',      
     '#ajax' =&gt; array(      
      'event' =&gt; 'click',
      'callback' =&gt; 'dropdown_callback',
      'wrapper' =&gt; 'dropdown-second-replace',
    ),
    );  

  $form['dropdown_second'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; 'Dynamic Dropdown',    
    '#prefix' =&gt; '&lt;div id="dropdown-second-replace"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',    
    '#options' =&gt; get_dropdown_options($field_val),
    '#default_value' =&gt; isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
  );  

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Submit'),
    '#prefix' =&gt; '&lt;br&gt;&lt;br&gt;&lt;div id="submit_btn"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    );
    return $form;
}

function dropdown_callback($form, $form_state) {        
    return $form['dropdown_second'];
}

function get_dropdown_options($key = '') {
  $options = array(
    t($key) =&gt; drupal_map_assoc(array(t($key))),    
  );
  if (isset($options[$key])) {
    return $options[$key];
  }
  else {
    return array();
  }
}

function dropdown_submit($form, &amp;$form_state) {
  print_r($form_state['values']); exit();  
}
you should merge the existing vales in $form['dropdown_second']['#options'] with the one in $form_state['values']['field_name']

something like this:

$existing_options = isset($form['dropdown_second']['#options']) ? $form['dropdown_second']['#options'] : array();
$new_option = get_dropdown_options($field_val);

$form['dropdown_second'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; 'Dynamic Dropdown',    
    '#prefix' =&gt; '&lt;div id="dropdown-second-replace"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',    
    '#options' =&gt; array_merge($existing_options, $new_option),
    '#default_value' =&gt; isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
  );