Prevent caching of ajax loaded forms Trying to make a custom page to add taxonomy terms. The page contains one drop-down menu that lists all enabled vocabularies. Full taxonomy add form loaded after I select some from that menu.

This is menu callback:

    function add_term_form_callback($form, $form_state) {

      module_load_include('inc', 'taxonomy', 'taxonomy.admin');
      $vid = $form_state['values']['vocabulary'];
      $vocabulary = taxonomy_vocabulary_load($vid);

      $form_state['values'] = array();
      $form_state['build_info']['args'] = array(array(), $vocabulary);

      $form_state['rebuild'] = TRUE;
      $form_state['no_cache'] = TRUE;
      $form = drupal_build_form('taxonomy_form_term', $form_state);

      return $form;

    }


...and form

    function add_term_form($form, &amp;$form_state) {

      $options = array();
      $options[0] = t('- Select -');
      $vocabularies = taxonomy_get_vocabularies();
      foreach ($vocabularies as $vocabulary) {
        $options[$vocabulary-&gt;vid] = $vocabulary-&gt;name;
      }

     $form['vocabulary'] = array(
       '#type' =&gt; 'select', 
       '#title' =&gt; t('Select vocabulary'), 
       '#options' =&gt; $options, 
       '#default_value' =&gt; $selected,
       '#ajax' =&gt; array(
         'callback' =&gt; 'add_term_form_callback', 
         'wrapper' =&gt; 'container',
         'event' =&gt; 'change',
       ),
     );

      $form['container'] = array(
       '#type' =&gt; 'container',
       '#prefix' =&gt; '&lt;div id="container"&gt;', 
       '#suffix' =&gt; '&lt;/div&gt;',
      );

      return $form;
    }


So the problem is that the form remains in the same state after first load. How to refresh the form properly?
According to the documentation:


  Changes to the form must only be made in the form builder function (ajax_example_autocheckboxes() in the example here), or validation will fail. The callback function must not alter the form or any other state.


You have been changing the form_state and the form in the callback.

Based on how I understood the documentation, you should be just returning the whole form in your ajax callback and then do the change in your form in the form builder function. You can just check the form_state value of the drop down to know which form to render. I imagine it'll be something like this:

function add_term_form_callback($form, $form_state) {
  return $form;
}

 function add_term_form($form, &amp;$form_state) {

 // If vocabulary has value
      // do stuff
      // $form = drupal_get_form('taxonomy_form_term');
 // else :
  $options = array();
  $options[0] = t('- Select -');
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $vocabulary) {
    $options[$vocabulary-&gt;vid] = $vocabulary-&gt;name;
  }

 $form['vocabulary'] = array(
   '#type' =&gt; 'select', 
   '#title' =&gt; t('Select vocabulary'), 
   '#options' =&gt; $options, 
   '#default_value' =&gt; $selected,
   '#ajax' =&gt; array(
     'callback' =&gt; 'add_term_form_callback', 
     'wrapper' =&gt; 'container',
     'event' =&gt; 'change',
   ),
 );

  $form['#prefix'] = '&lt;div id="container"&gt;';
  $form['#suffix'] = '&lt;/div&gt;';
  );
  // endif; 

  return $form;
}


EDIT: I just noticed that you weren't wrapping the form but a container in the form. you would have to wrap the form if you want the whole form to be reloaded.