Dependent Dropdown based on Database Queries 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:

-&gt;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:

&lt;?php

function docappt_states_form($form, &amp;$form_state) {

// Grab the list of specialties from the DB

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

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


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

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

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


  return $form;
}


function docappt_dependent_dropdown_callback($form, $form_state) {
  return $form['filtereddoc'];
}
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.