Results of autocomplete field in form depending on another field - Drupal 7 I have an autocomplete field in a Drupal form made with Form API. It works great, suggesting the data from a db_query.

However, I would like to alter this db_query depending on the selection made in another field of the form (a select field).

But I don't know how to do it, as I would need to change the db_query parameters on-the-fly (Javascript) but they are hard-coded in PHP!!

Is what I want possible at all? (it can be done in pure HTML + Javascript + AJAX PHP but I cannot imagine how to do it in Drupal).
Keep a wild card in your autcomplete menu path something like

function mymodulename_name_menu() {
  $items['example/autocomplete/%/%'] = array(
    'page callback' =&gt; '_mymodulename_autocomplete_callback',
    'page arguments' =&gt; array(2, 3),
    'access callback' =&gt; TRUE,
    'type' =&gt; MENU_CALLBACK,
  );
}


In your form

function mymodulename_my_form($form, &amp;$form_state) {
// Your select field.
  $form['select_field'] = array(
    '#type' =&gt; 'select',
    '#options' =&gt; array(
      'option1' =&gt; 'Option1',
      'option2' =&gt; 'Option2',
    ),
    '#ajax' =&gt; array(
      'callback' =&gt; '_mymodulename_ajaxcallback',
      'wrapper' =&gt; 'replace_autocomplete_field',
    ),
  );
  // A container field
  $form['autocomplete_field_container'] = array(
    '#type' =&gt; 'container',
    '#tree' =&gt; TRUE,
    '#prefix' =&gt; '&lt;div id="replace_autocomplete_field"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );
  $query_parameter = isset($form_state['values']['select_field']) ? $form_state['values']['select_field'] : 'default_value_to_start_with';
  // Your autocomplete field.
  $form['autocomplete_field_container']['autocomplete_field'] = array(
    '#title' =&gt; t('My autocomplete field.'),
    '#type' =&gt; 'textfield',
    '#autocomplete_path' =&gt; "example/autocomplete/$query_parameter",
  );
}


You Automcomplete menu callback

function _mymodulename_autocomplete_callback($query_parameter, $string) {
  // Use $query parameter to decide autocomplete field behavior.
}


Basically I am trying to update the autocomplete field by attaching '#ajax' behaviour to select field, and while updating the autocomplete field we will change the query parameter which db_query will use.