By kosamo on
Hello all
I'd like to ask if there is a way to modify dynamically the options of a select form item according to the selection made on another select form item. These options are retrieved from a table in the database.
I am not familiar to jquery (less to ajax), could anyone suggest me an idea for this ?
Thanks
Comments
An Example
Here's some JQuery code I've used to do this. In my example when a section select is changed, a category select will be populated with options looked up via AJAX. So when the select with the ID "edit-gstimeout_admin_add-categorize-section" is changed, an AJAX request is made to "/ajax/categories" for the child categories within the section. The select with the ID "edit-gstimeout_admin_add-categorize-category" is then populated with the resulting JSON data.
Note that, in my example, there is a DIV wrapper around the category selector that allows the select element to be faded out then in (and hidden when it's empty).
Hope this helps.
JQuery stuff is fun, but a bit mind-bending when you first get into it. :)
-Mike Cantelon
http://mikecantelon.com
...
BTW, doesn't Drupal complain "an illegal choice has been detected..." when you submit this form? AFAIK, it's supposed to complain, because the user selects some <OPTION> which wasn't seen by drupal_get_form et al. You can circumvent this by doing
'#DANGEROUS_SKIP_CHECK' => TRUEon the form element (alternatively, it can be defined as a textfield).Uff :(
But you don't need to do HTML stuff in your code. You could go along the lines of:
BTW, doesn't Drupal complain
For whatever reason I haven't ran into that.
Definitely cleaner... Thanks!
-Mike Cantelon
http://mikecantelon.com