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).

2 Answers 2


Keep a wild card in your autcomplete menu path something like

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

In your form

function mymodulename_my_form($form, &$form_state) {
// Your select field.
  $form['select_field'] = array(
    '#type' => 'select',
    '#options' => array(
      'option1' => 'Option1',
      'option2' => 'Option2',
    '#ajax' => array(
      'callback' => '_mymodulename_ajaxcallback',
      'wrapper' => 'replace_autocomplete_field',
  // A container field
  $form['autocomplete_field_container'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<div id="replace_autocomplete_field">',
    '#suffix' => '</div>',
  $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' => t('My autocomplete field.'),
    '#type' => 'textfield',
    '#autocomplete_path' => "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.

  • Thank you very much. As I see, your response is similar to that in this tutorial: complexdan.com/passing-custom-arguments-drupal-7-autocomplete . I will try your specific example in any case, I found the tutorial by looking for strings in your example, but I will try if there is any conceptual difference. Thank you again.
    – Cesar
    Sep 18, 2015 at 15:23
  • I am glad it helped you :)
    – arpitr
    Sep 18, 2015 at 15:24

I have found this reference: How to dynamically reconfigure Drupal's jQuery-based autocomplete at runtime? However, I see it somehow complicated. I prefer this approach of mine that I am using right now (and working for an exposed filter):

I create a hook to add a Javascript Ajax call to the select list. In that Ajax call, I use a cookie associated to the user, and the value of the selected value in the select list, and insert both in a database table.

Now when I make the Autocomplete call, in the db_query I use the user cookie and read the value inserted, so I can restrict the suggestions based on that value.

The only problem is that the call is asynchronous, but if the autocomplete function cannot find the cookie value in the table, will yield a warning in the suggestion list, indicating to type more letters or type again, giving time to the first Ajax call to complete.

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.