Using ajax to filter drupal autocomplete form I'm trying to build a custom search form, and I want to limit the scope of the search with a dropdown select menu, ie. choose "jewelry" in the dropdown menu and the autocomplete form will only return matches of that category.

Here's what I have so far:

function mymodule_search_menu() {
$items['mymodule_search-autocomplete-test'] = array(
'title' =&gt; 'Test autocomplete',
'page callback' =&gt; 'drupal_get_form',
'page arguments' =&gt; array('category'),
'access arguments' =&gt; array('view published content'),
'type' =&gt; MENU_NORMAL_ITEM,
);

$items['mymodule_search-autocomplete-engine'] = array(
 'page callback' =&gt; 'mymodule_search_autocomplete',
 'access arguments' =&gt; array('view published content'),
 'type' =&gt; MENU_CALLBACK,
);
 return $items;
 }

function mymodule_search_form($form, &amp;$form_state) {
$form = array();

$form['category'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; 'First Dropdown',
    '#options' =&gt; array('All','Product'),
    '#ajax' =&gt; array(
    'callback' =&gt; 'mymodule_search_ajax_callback',
    'wrapper' =&gt; ???,
    'method' =&gt; ???,
    ),
);

$form['search_autocomplete'] = array(
'#title' =&gt; t('Search'),
'#type' =&gt; 'textfield',
'#maxlength' =&gt; 60,
'#autocomplete_path' =&gt; 'mymodule_search-autocomplete-engine',
'#prefix' =&gt; '&lt;div id="searchform"&gt;',
'#suffix' =&gt; '&lt;/div&gt;',
 );

$form['submit'] = array(
'#type' =&gt; 'submit',
'#value' =&gt; 'Søg alt',
 );

 return $form;
 }

function mymodule_search_autocomplete($text) {
$results = array();
//query products
$query_products = db_select('commerce_product', 'p');
$query_products
-&gt;condition('p.title', '%' . db_like($text) . '%', 'LIKE')
-&gt;fields('p', array('title'))
-&gt;orderBy('title', 'ASC');
$res = $query_products-&gt;execute();
while ($res1 = $res-&gt;fetchAssoc()) {
  $matches[1][$res1['title']] .= check_plain($res1['title']);
}

array_unshift($matches[1], "PRODUCTS");

//query content
$query_content = db_select('node', 'n');
$query_content
-&gt;condition('n.title', '%' . db_like($text) . '%', 'LIKE')
-&gt;fields('n', array('title'))
-&gt;orderBy('title', 'ASC');
$res = $query_content-&gt;execute();
while ($res2 = $res-&gt;fetchAssoc()) {
  $matches[2][$res2['title']] = check_plain($res2['title']);
}

array_unshift($matches[2], "CONTENT");

$matches = mymodule_search_flatten($matches);
$newmatches = array();
foreach ($matches as $match){
$newmatches[$match] = $match;
}

drupal_json_output($newmatches);
}

function mymodule_search_ajax_callback($form, $form_state) {
  ???
}


As you might see, I have 2 different querys in my autocomplete function, and (I think) I would like to limit the result to only the selecteded querys with help of an ajax call from the dropdown category.

Oh, and don't mind all the "unshifting" and looping that goes on in the autocomplete function - I have to have category headers in my result, and this was the only way I could think of to do that.

If there's a better way to do this, or if you know how to accomplish this, please let me know.

Thanks!!
I know it's too late but for others who might find this questions, the answers are here: 

form auto complete how to restrict results by previous drop down

and

Dynamic select list in the form (dependent dropdown)