Cakephp下拉菜单在刷新页面时不显示正确的数据 [英] Cakephp Dropdowns not showing right data when Refreshing the page

查看:125
本文介绍了Cakephp下拉菜单在刷新页面时不显示正确的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个下拉列表中列出了一个国家/地区和其他我的页面上的城市。
当我最初加载页面时,我将国家设置为从用户注册加载的默认国家。然后我加载该国家的城市。

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.

任何帮助将非常感激。

em>更新 -
这里是我的控制器代码

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');
        }
    }

}

我所遇到的问题是,在页刷新国家下拉菜单的选定属性设置不正确。 Ive检查控制器的值正确,即使我刷新页面。

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.

在此变量@selectedCountryCode中设置所选的国家/地区值。

Im setting the selected country value in this variable @selectedCountryCode.

推荐答案

p>你不应该使用刷新来做到这一点,这使得控制器逻辑太复杂。使用javascript根据国家/地区列表更改城市列表。您可以使用ajax来获取城市列表。

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.

这篇关于Cakephp下拉菜单在刷新页面时不显示正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆