Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Drupal 6: Using AHAH to dynamically generate form elements (and integration with multi-tiered taxonomy)
For some time now I’ve wanted to write a blog entry about using AHAH to create dynamically generated form elements. After a recent conversation at work regarding usability, I now had a real world example to create: how to use tiered taxonomy to dynamically generate a form. This code snippet will show you how to create a form that creates child select dropdowns based on the parent taxonomy term the user selects.
First I established a multi-tier taxonomy called “AHAH”:
For this example I created a menu callback to display my initial form:
I then defined the page callback to show the initial form:
The above form callback produces the following:
Next I defined a callback to handle the AHAH page request:
The above code allows the user to select an option from the top level tier of taxonomy and the AHAH callback will generate the a select dropdown of the child taxonomies as shown below:
On form submission, you’ll see that the options the user selected as stored in $form_state[‘values’][‘select_1’] and $form_state[‘values’][‘select_2’]