I have made textfield to be autocomplete field by using hook_form_alter.
mymoudle_form_alter(&$form, &$form_state, $form_id){
$form['field_state']['und'][0]['value']['#autocomplete_path'] ='get-state-values';
}
function mymoudle_drupal_menu() {
$items = array();
$items['get-country-value'] = array(
'title' => 'Country Value',
'page callback' => '_get_country_value',
//'page arguments' => array(1),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['get-state-values'] = array(
'page callback' => '_get_state_values',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
function _get_state_values($string){
$country =$_SESSION['country'];
if($country == 'India'){
$state = array('Tamilnadu' => 'Tamilnadu','Karanataka' => 'Karanataka');
}
else{
$state = array('NewYork' => 'NewYork','Manahattan' => 'Manahattan');
}
print drupal_json_output($state);exit;
}
function _get_country_value(){
$country = $_REQUEST['valueSelected'];
$_SESSION['country'] = $country;
print drupal_json_output($country);exit;
}
If I select country as India and type in the state as 'n' it shows me in auto-complete as two states with n words like tamilnadu and karnataka which is correct(it runs ajax to fetch the values as shown in screenshot-1).
The issue over here is if I again change the country to 'Us' and type same 'n' it shows same set of results and it doesn't run the ajax(as shown in screenshoot-2). Its showing from cache I think for previous results.
NOTE: I want an fix for the ajax caching issue.
I have attached the screenshot of what's happening .How to avoid this issue?.Any help will be much appreciated.Thanks in advance.