如何更新CakePHP中的$ ajax-> remoteFunction()以更改省和城市的组合框?

时间:2022-10-20 15:52:34

I have two combos; provinces and cities. I would like to change cities value when the province combo value changes. Here is my code

我有两个组合;省市。我想在省组合值变化时改变城市价值。这是我的代码

<div class="cities form">
<?php
    $v = $ajax->remoteFunction(array('url' => '/cities/','update' => 'divcity'));
    print $form-> input('Province.province_id', array('type' => 'select', 'options'=> $provinces, 'onChange' => $v)); 
?>  
<div id="divcity">
<?php
    echo $form->input('Cities.cities_name'); 
?>
</div>

Every time I change province combo, it call cities/index.ctp. anybody want to help? really thank for your help wawan

每次我更改省组合时,都会调用cities / index.ctp。有人想帮忙吗?非常感谢你的帮助wawan

2 个解决方案

#1


2  

The 'url' => '/cities/' is calling the default index action of the cities controller.

'url'=>'/ cities /'正在调用cities控制器的默认索引操作。

This automatically renders the cities/index.ctp view.

这会自动呈现cities / index.ctp视图。

Have you included the RequestHandler component in the cities controller?

您是否在CITY控制器中包含了RequestHandler组件?

This can be used to detect Ajax requests and then render a different view.

这可用于检测Ajax请求,然后呈现不同的视图。

#2


0  

You need to first include the RequestHandler Component at the top of the CitiesController, then write a function to list cities, optionally requiring a Province's id.

您需要首先在CitiesController的顶部包含RequestHandler组件,然后编写一个函数来列出城市,可选择要求省的ID。

I think you'll end up having something like this:

我想你最终会得到这样的东西:

<?php
// In the view
$v = $ajax->remoteFunction(array('url' => '/cities/list','update' => 'divcity'));
print $form-> input('Province.province_id', array('type' => 'select', 'options'=> $provinces, 'onChange' => $v));

// In CitiesController
function list($province_id = null) {
    // use $this->City->find('list', array('fields'=>array('City.id', 'City.name'))) 
            // to generate a list of cities, based on the providence id if required
    if($this->RequestHandler->isAjax()) {
        $this->layout = 'ajax';
        $this->render();
    }
} ?>

#1


2  

The 'url' => '/cities/' is calling the default index action of the cities controller.

'url'=>'/ cities /'正在调用cities控制器的默认索引操作。

This automatically renders the cities/index.ctp view.

这会自动呈现cities / index.ctp视图。

Have you included the RequestHandler component in the cities controller?

您是否在CITY控制器中包含了RequestHandler组件?

This can be used to detect Ajax requests and then render a different view.

这可用于检测Ajax请求,然后呈现不同的视图。

#2


0  

You need to first include the RequestHandler Component at the top of the CitiesController, then write a function to list cities, optionally requiring a Province's id.

您需要首先在CitiesController的顶部包含RequestHandler组件,然后编写一个函数来列出城市,可选择要求省的ID。

I think you'll end up having something like this:

我想你最终会得到这样的东西:

<?php
// In the view
$v = $ajax->remoteFunction(array('url' => '/cities/list','update' => 'divcity'));
print $form-> input('Province.province_id', array('type' => 'select', 'options'=> $provinces, 'onChange' => $v));

// In CitiesController
function list($province_id = null) {
    // use $this->City->find('list', array('fields'=>array('City.id', 'City.name'))) 
            // to generate a list of cities, based on the providence id if required
    if($this->RequestHandler->isAjax()) {
        $this->layout = 'ajax';
        $this->render();
    }
} ?>