Searching for cascading ajax dropdown in this forum only gives me two discussions here and the other here

I have an almost similar issue only that the form select element I'm about to create cascades down in the 'taxonomy tree'. So each dropdown that was ajaxed will again be used to trigger another dropdown--in ajax...

Here is my sample module (trimmed down to relevant parts only):

function mainswitchboard($form,&$form_state){
    $form = array('#prefix'=>'', '#suffix'=>'');
    $form['maincategory'] = array(
        '#title'=>'Firs Select',
    return $form;

function maincategory_callback($form,$form_state){

   $from_element_name = $form_state['triggering_element']['#name'];

    $triggerer = $from_element_name;
    $value = $form_state['values'][$triggerer];

    drupal_set_message(time(). ' ' .$triggerer);

    $commands = array(); $arguments = array();
    if(count(_populate_dropdowns($value))) {
        $form['subcat_for_'.$value] = array(
            '#title'=>'Subcat for '.$value,


         $arguments['ajax'] = array(


        $commands[] = ajax_command_settings($arguments,TRUE);


    $commands[] = ajax_command_insert(NULL, drupal_render($form));
    $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
    return array('#type'=>'ajax','#commands'=>$commands);


The above code seems to work the only problem is you the second dropdown no longer produce results (although a throbber is showing when you click the second dropdown..) The second dropdown does not seem to honor the arguments variable, hence, it does not seem to call the function in its callback...

I'm stuck new drupaler...


  • 1
    the callback function should do nothing but only return the $form (or parts of the form). Any logic code should be in form api (mainswitchboard).
    – gilzero
    Commented Nov 12, 2012 at 3:32
  • Thank you @gilzero that makes sense. Will keep it in mind.
    – jan
    Commented Dec 21, 2012 at 6:51

2 Answers 2


You cannot change your $form in your ajax callback. Instead try to add a validate function with hook_form_alter on your form (or if in custom form just there) where your add a variable to your $form_state and don't forget $form_state['rebuild'] = TRUE. This will cause your form to rebuild (so form build function is called anew) In your form build function you can check this variable in $form_state and malipulate the form. In your ajax callback you need to replace the form element (ajax_command_replace)

So workflow will be: form build function - ajax trigger - form build function - form validation (set variable & rebuild) - form build function - ajax callback

  • Thanks @Filip. Unbelievable my PC crashed including my previous projects no longer recovered. So i have to start anew from scratch and took me a while to get back due to some other stuffs as well. Now I managed to come back and my own project is finishing up, constantly reminding my self to back up to external disk :D. Will try out your solution and will let you know.
    – jan
    Commented Dec 21, 2012 at 6:52

Following URL helped me to fix this issue.

Issue was rebuilding second select list.

Dependent drop down list

Your Answer

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

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