views:

1937

answers:

3

Hi All

I am a newbie in cakephp; I have two combos, provinces and cities with cakephp. 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

+2  A: 

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

This automatically renders the cities/index.ctp view.

Have you included the RequestHandler component in the cities controller?

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

neilcrookes
A: 

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.

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();
 }
} ?>
A: 

CakePHP AJAX country region select box (combo box) DEMO and source files :

http://www.croyantdebridiers.com/?p=52