2

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'];
}
0

So try to use

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),
        '#prefix'   => '<div id="edit-industry-lens-term">',
        '#suffix'=> '</div>'
    );  

    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']);

    return $form['industry_lens_term'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.