I've been working through the Drupal 7 AJAX form examples in the Examples module, and am trying to set up a dependent dropdown form element with 2 select lists populated from the database.

Basically, I have Select called Specialties, that pulls a list of items, and a second Select called Physicians—which should be populated with Physicians tagged (via Node Reference field) with the chosen specialty. So, for example, the end user selects "Back" from the Specialties select list, and the second should populate with a list of the 3 physicians containing this Node Reference.

From what I can tell, both queries are returning the right types of nodes, and if i place a condition in the Physicians query to hard-code the specialty field:

->condition('field_specialty_nodereference_nid', '90')

it populates the second list with all doctors tagged with that nodeference.

What I am struggling with is how to make that condition dynamic, so a value from the first select is passed through to the second on change. Thank you in advance for any help you can provide.

Here is my code:


function docappt_states_form($form, &$form_state) {

// Grab the list of specialties from the DB

  $query = db_select('node', 'n')
  ->fields('n', array('nid','title'))
  ->condition('type', 'specialty')
  ->orderBy('title', 'ASC');
  $results = $query->execute()->fetchALL();

  //define rows
  $specialtylist = array();
  foreach ($results as $result) {
      $specialtylist[$result->nid] = $result->title;

// Let's call some docs by specialty reference
  $query = db_select('node', 'n');
  $query->join('field_data_field_specialty_nodereference', 'specnr', 'n.nid = specnr.entity_id');
  ->fields('n', array('nid','title'))
  ->fields('specnr', array('field_specialty_nodereference_nid', 'entity_id'))
  ->condition('type', 'physician_profile');
  $docresults = $query->execute()->fetchALL();

  //define rows
  $docfoptions = array();
  foreach ($docresults as $docresult) {
      $docfoptions[$docresult->nid] = $docresult->title;

  $specialties = isset($form_state['values']['specialties']) ? $form_state['values']['specialties'] : key($specialtylist);
  $form['specialties'] = array(
    '#type' => 'select',
    '#title' => 'Specialties',
    '#options' => array(
      0 => t('- Select -')) + $specialtylist,
    '#default_value' => isset($form_state['values']['specialties']) ? $form_state['values']['specialties'] : 0,
    '#ajax' => array(
        'callback' => 'docappt_dependent_dropdown_callback',
        'wrapper' => 'filtered_doc_list',
        'event' => 'change',
        'method' => 'replace',
  $filtereddoc = isset($form_state['values']['filtereddoc']) ? $form_state['values']['filtereddoc'] : key($docfoptions);
  $form['filtereddoc'] = array(
    '#type' => 'select',
    '#title' => $specialtylist[$specialties] . ' ' . t('Doctors'),
    '#prefix' => '<div id="filtered_doc_list">',
    '#suffix' => '</div>',
    '#options' => $docfoptions,

  return $form;

function docappt_dependent_dropdown_callback($form, $form_state) {
  return $form['filtereddoc'];
  • I'm pretty sure the AJAX is call is working correctly, as the #title element for the 'filtereddoc' select element updates correctly, showing the specialty name + Doctors, e.g., select "Back" from the specialties dropdown, and the title of "filtereddoc" changes to "Back Doctors".
    – michaelaj
    Commented Jun 10, 2014 at 20:55

2 Answers 2


use FF or Chromes console.log function and be sure your Ajax is being called properly. Or use dd() from the devel module to log whats going on when the change event or other JS actions are called.


Just a follow up...this was fixed by modifying the second query as follows:


->condition('type', 'physician_profile');
  $docresults = $query->execute()->fetchALL();


->condition('type', 'physician_profile');
  if ( !empty($form_state['values']) && !empty($form_state['values']['specialties']) ) {
     $query->condition('field_specialty_nodereference_nid', $form_state['values']['specialties']);
  $docresults = $query->execute()->fetchALL();

Thanks to drupal.org user nevets for the solve.

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.