Ive two dropdown lists one for country and other for cities on my page. When i load the page initially i set the country to the default country loaded from the user registeration. Then i load the cities for that country.
The problem is that now when i change the country dropdown and refresh the page my country dropdown selected value remains the same which has changed but the cities are loading from the default country.
I'm getting the right value at the view from the controller but view is not setting it right. The code in my view is as follows
echo $this->Form->input('from_country_code',
array(
'options'=>$countries,
'id'=>'from_country_code',
'label' => __('Country',true),
'selected'=>$selectedCountryCode
)
);
Secondly, please explain me that how can i detect that a page is refreshed and retain the changed drop down values and don't execute the whole action controller code.
Any help would be highly appreciated.
Updated -- Here its my controller code
function add() {
$currentUser = $this->Auth->user();
$countryMap = $this->requestAction('/countries/getList/');
$this->set('countries',$countryMap);
$this->set('selectedCountryCode',$currentUser['City']['countriesCode']);
$cityMap = $this->requestAction('/cities/getListByCountryCode/'.$currentUser['City']['countriesCode']);
$this->set('cities',$cityMap);
if(!empty($this->data)) {
if($this->Request->saveAll($this->data)) {
$this->Session->setFlash('The Request was successfully Posted');
$this->redirect(array('controller'=>'users','action'=>'requests'));
} else {
$this->Session->setFlash('The Request was not saved. Please try again');
}
}
}
Please note that the issue i'm having is that on page refresh the selected property of the country dropdown is not setting properly. Ive checked the value from controller its coming correctly even if i refresh the page.
Im setting the selected country value in this variable @selectedCountryCode.
You shouldn't use refresh to do that, that makes the controller logic too complicated. Use javascript to change the cities list based on the country list. You may want to use ajax to get the cities list.
I'm not sure to understand. You have two dropdown lists on the same page and what you want to do is to update the elements of the second one according to the selected element in the first one ? And therefore the user will have to hit the refresh button to update the second list ?
If this is what you mean, I think this is not user friendly at all and even not a standard way to navigate on internet. On top of that, this is absolutely normal that the whole controller action is executed again when you hit the refresh button. In fact hitting the refresh button means exactly 'please execute the webpage logic again'.
If you want a more user friendly way to update the second dropdown list, you should search for a Javascript way to do it. See for instance: http://nuts-and-bolts-of-cakephp.com/2010/03/10/use-cakephp-jquery-to-build-dynamic-selects/