form auto complete how to restrict results by previous drop down Hi I have a select field on a form which contains countries. The next field is a text box  which is driven by an autocomplete function. The autocomplete function returns cities from a database table.

I want to restrict the cities that the autocomplete will return based on the country value selected in the dropdown. How do I do this?

if it helps here is my form

 $country_list = _get_country_list();
    $form['country'] = array(
            '#type'=&gt;'select',
            '#title'=&gt;'Select Country',
            '#description'=&gt;'Select the country where you want to collect the car',
            '#options'=&gt;$country_list,
    );
$form['officename'] = array(
    '#type'=&gt;'textfield',
    '#title'=&gt; t('Location'),
    '#default_value'=&gt;$form_state['officename'],
    '#size'=&gt;20,
    '#maxlength' =&gt; 50,
    '#description' =&gt; t('Select the country where you want to pick up the car'),
    '#attributes' =&gt; null,
    '#required' =&gt; TRUE,
            '#autocomplete_path'=&gt;'location-autocomplete'
);


$form['submit'] = array(
'#type'=&gt;'submit',
'#value'=&gt;'Search Now',
'#submit'=&gt;array('HolidayAutosXML_carsearch_form_submit'),
);


and here is my autocomplete function. I want to restrict the sql with a where country = {country selected in Drop Down List}

function location_autocomplete($string){
$matches = array();
$result = db_query("select officename from {holidayautos_country_list} where officename like :keyword limit 0, 10", array(':keyword'=&gt;"%" . db_like($string) . "%"));
 foreach($result as $row) {
    $matches[$row-&gt;officename] = check_plain($row-&gt;officename);
  }
drupal_json_output($matches);
}


Many Thanks
Simon
Using the links suggested from Anil Sagar, I put together this solution using Ajax.

function HolidayAutosXML_carsearch_form($node, &amp;$form_state) {
  $country_list = _get_country_list();
  $form['country'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; 'Select Country',
    '#description' =&gt; 'Select the country where you want to collect the car',
    '#options' =&gt; $country_list,
    '#ajax' =&gt; array(
      'callback' =&gt; 'update_officename_autocomplete',
      'wrapper' =&gt; 'haofficename',
    ),
  );

  $form['officename'] = array(
'#type' =&gt; 'textfield',
'#title' =&gt; t('Location'),
'#default_value' =&gt; $form_state['office name'],
'#size' =&gt; 20,
'#maxlength' =&gt; 50,
'#description' =&gt; t('Select the location where you want to pick up the car'),
'#attributes' =&gt; NULL,
'#required' =&gt; TRUE,
    '#autocomplete_path' =&gt; 'location-autocomplete',
    '#prefix' =&gt; '&lt;div id="haofficename"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  if (!empty($form_state['values']['country'])) {
    $form['officename']['#autocomplete_path'] = "location-autocomplete/{$form_state['values']['country']}";
  }

  $form['submit'] = array(
    '#type'=&gt;'submit',
    '#value'=&gt;'Search Now',
    '#submit'=&gt;array('HolidayAutosXML_carsearch_form_submit'),
  );

  return $form;
}



I added an Ajax element to the country drop down, telling it to look for a &lt;div&gt; with an ID equal to "haofficename" and calling a function named update_officename_autocomplete().
I used a prefix and suffix element in the office name element to wrap it in a &lt;div&gt; element with the ID haofficename, which Ajax is going to look for.
update_officename_autocomplete() simply returns the altered office name element of the form. if you look inside the if statement you will see that it is adding to the #autocomplete_path property. So I had to add a second parameter to my autocomplete function. This function now takes $country and $officename.

function location_autocomplete($country,$string){
   $matches = array();
   $result = db_query("select officename from {holidayautos_country_list}        where officename like :keyword and country = :country limit 0, 10",  array(':keyword'=>"%" . db_like($string) . "%",':country'=>$country));
  foreach($result as $row) {
  $matches[$row->officename] = check_plain($row->officename);
  }
  drupal_json_output($matches);
  }


It works like a charm.