Drupal AJAX Framework with Multiple Instances of a Form

img20141125094913I recently encountered a unique issue with AJAX-ified form elements in Drupal. We’re building a site that shows the same form twice on a single page. I’m rendering both forms in two separate blocks; one appears at the top of the page, and one appears at the bottom.

The Select a Location field has an ajax callback to replace the the Select a Program select list with programs available at the chosen location. Initially, the top form worked as expected, but the bottom form would never get an updated Select a Program field. The AJAX callback seemed to run, but it would not replace the select list.

After fixing (by adding a unique, unchanging identifier, both forms performed as expected; changing the Select a Location dropdown updates the Select a Program dropdown, and the two forms do not interfere with one-another.

Originally I tried using $form['#id']  because it looked cleaner, but it didn’t work correctly. The Select a Program list would update, but it’d have the same values every time, no matter which location was chosen.

Leave a Reply

Your email address will not be published.