I have a very simple form that has a select box of the terms in a vocabulary, and renders the selected term below it. It seems that the first time I change the value in the select it renders the appropriate term, however every subsequent “change” event it uses what ever term I selected the first time.
The code is as follows
function jms_industry_lens_form($form, &$form_state) { $values = array(); // Load the vocabulary and collapse into an array that form api understands $vocabulary = taxonomy_vocabulary_machine_name_load('industry_lens'); $terms = taxonomy_get_tree($vocabulary->vid); foreach($terms as $term) { $values[$term->tid] = $term->name; } $form['industry_lens'] = array( '#type' => 'select', '#options' => $values, '#default_value' => 0, '#required' => TRUE, '#ajax' => array( 'callback' => 'jms_industry_lens_form_ajax', 'wrapper' => 'edit-industry-lens-term', ), ); $form['industry_lens_term'] = array( '#type' => 'item', '#markup' => _jms_render_term($terms[0]->tid), ); return $form; } /** * Ajax callback to change the rendered industry lens taxonomy term */ function jms_industry_lens_form_ajax(&$form, &$form_state) { $form['industry_lens_term']['#markup'] = _jms_render_term($form_state['values']['industry_lens']); $form_state['rebuild'] = TRUE; return $form['industry_lens_term']; }
Sponsored by SupremePR