0

I have a select element in my form.

function myid_print_user_page_form($form, &$form_state)
{
    $form = array();
    $form['options'] = array(
        '#type' => 'select',        
        '#options' => array(
            0 => t('All'),
            1 => t("Printed ID's"),
            2 => t("Unprinted ID's"),
        ),
        '#default_value' => 0,             
        '#ajax' => array(
            'callback' => 'myid_print_all_submitted_id_callback',
            'wrapper' => 'myid_print_search_idnumber_wrapper',
        ),                             
    );

    $form['myid_print_search_idnumber_wrapper'] = array(
       '#type' => 'item',      
       '#markup' => '<div id= "myid_print_search_idnumber_wrapper"></div>'                               
    );
    return $form;
}   

I'm stuck in the callback function. It always return 1, even though I selected other options.

function myid_print_all_submitted_id_callback($form, $form_state)
{
    return $form_state['values']['options'];
}

Where am I missing?

1 Answer 1

1

You can use this code

function myid_print_user_page_form($form, &$form_state)
{
    $form = array();
    $form['options'] = array(
        '#type' => 'select',        
        '#options' => array(
            0 => t('All'),
            1 => t("Printed ID's"),
            2 => t("Unprinted ID's"),
        ),
        '#default_value' => 0,             
        '#ajax' => array(
            'callback' => 'myid_print_all_submitted_id_callback',
            'wrapper' => 'myid_print_search_idnumber_wrapper',
        ),                             
    );

    $form['myid_print_search_idnumber_wrapper'] = array(
       '#type' => 'item', 
    // When the form is rebuilt during ajax processing, the $selected variable
    // will now have the new value and so the options will change.     
       '#markup' => isset($form_state['values']['options']) ? $form_state['values']['options'] : '',     
       '#prefix' => '<div id="myid_print_search_idnumber_wrapper">',
       '#suffix' => '</div>',                              
    );
    return $form;
}   
function myid_print_all_submitted_id_callback($form, $form_state){
// rebuild the form
    return $form['myid_print_search_idnumber_wrapper'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.