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'=>'select',
'#title'=>'Select Country',
'#description'=>'Select the country where you want to collect the car',
'#options'=>$country_list,
);
$form['officename'] = array(
'#type'=>'textfield',
'#title'=> t('Location'),
'#default_value'=>$form_state['officename'],
'#size'=>20,
'#maxlength' => 50,
'#description' => t('Select the country where you want to pick up the car'),
'#attributes' => null,
'#required' => TRUE,
'#autocomplete_path'=>'location-autocomplete'
);
$form['submit'] = array(
'#type'=>'submit',
'#value'=>'Search Now',
'#submit'=>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'=>"%" . db_like($string) . "%"));
foreach($result as $row) {
$matches[$row->officename] = check_plain($row->officename);
}
drupal_json_output($matches);
}
Many Thanks Simon