In a view I have three inmputs add, name, country and Province, the
Countries Provinces the filter through the method jQuery
$. getJSON and it works to perfection.
When I apply the same code to Edit, I've seen THROUGH THE
Firebug that it returns no data jonson, I leave below
the code I use,, if someone could give me some idea.
I use CakePHP 1.3.8
Thanks

//views/cities/add.ctp
<div class="cities form">

    <?php echo $this->Html->script('list_provinces'); ?>
    <?php echo $this->Form->create('City'); ?>
    <fieldset>
        <legend><?php __('Add City'); ?></legend>
        <?php
        echo $this->Form->input('nombre');
        echo $this->Form->input('country_id', array('options' =>
$countries, 'empty' =>
            '-- Seleccione Pais --', 'id' => 'country'));
        ?>
        <div id="provincias" style="display: none;">
            <?php
            echo $this->Form->input('province_id', array('options' =>
$provinces, 'empty' =>
                '-- Seleciones Provincia --', 'id' => 'provincia'));
            ?>
        </div>

    </fieldset>
    <?php echo $this->Form->end(__('Submit', true)); ?>
</div>

//views/cities/edit.ctp
<div class="cities form">

<?php echo $this->Html->script('list_provinces'); ?>
<?php echo $this->Form->create('City');?>
        <fieldset>
                <legend><?php __('Edit City'); ?></legend>

        <?php
                echo $this->Form->input('id');
                echo $this->Form->input('nombre');
                 echo $this->Form->input('country_id', array('options'
=>
$countries, 'empty' =>
            '-- Seleccione Pais --', 'id' => 'country'));
        ?>
                <div id="provincias" >
            <?php
            echo $this->Form->input('province_id', array('options' =>
$provinces, 'empty' =>
                '-- Seleciones Provincia --', 'id' => 'provincia'));
            ?>
        </div>
        </fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>

//controllers/citiescontroller.ctp
<?php
class CitiesController extends AppController {

        var $name = 'Cities';
var $components = array('RequestHandler');

   public function obtener_provincias() {

  if($this->RequestHandler->isAjax()) {
    $this->set('provinces', $this->City->Province->find('list',
                           array('conditions' =>
                                       array('province.country_id' =>
$this->params['url']['countryId']),
                                 'recursive' => -1)));

  }

}

        function index() {
                $this->City->recursive = 0;
                $this->set('cities', $this->paginate());
        }

        function view($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid city',
true));
                        $this->redirect(array('action' => 'index'));
                }
                $this->set('city', $this->City->read(null, $id));
        }

        function add() {
                if (!empty($this->data)) {
                        $this->City->create();
                        if ($this->City->save($this->data)) {
                                $this->flashSuccess('la ciudad The
city has been saved','index');
                                /*$this->Session->setFlash(__('The
city has been saved', true));
                                $this->redirect(array('action' =>
'index'));
                                */
                        } else {
                                $this->Session->setFlash(__('The city
could not be saved. Please,
try again.', true));
                        }
                }
                $countries = $this->City->Country->find('list');
                $provinces = $this->City->Province->find('list');
                $this->set(compact('countries', 'provinces'));

        }

        function edit($id = null) {
                if (!$id && empty($this->data)) {
                        $this->Session->setFlash(__('Invalid city',
true));
                        $this->redirect(array('action' => 'index'));
                }
                if (!empty($this->data)) {
                        if ($this->City->save($this->data)) {
                        //$this->flashSuccess('la ciudad The city has
been saved','index');
                           $this->Session->setFlash(__('The city has
been saved', true));
                           $this->redirect(array('action' =>
'index'));
                        } else {
                                $this->Session->setFlash(__('The city
could not be saved. Please,
try again.', true));
                        }
                }
                if (empty($this->data)) {
                        $this->data = $this->City->read(null, $id);
                }
                $countries = $this->City->Country->find('list');
                $provinces = $this->City->Province->find('list');
                $this->set(compact('countries', 'provinces'));
        }

        function delete($id = null) {

                if (!$id) {
                        $this->Session->setFlash(__('Invalid id for
city', true));
                        $this->redirect(array('action'=>'index'));
                }
                if ($this->City->delete($id)) {
                        $this->Session->setFlash(__('City deleted',
true));
                        $this->redirect(array('action'=>'index'));
                }
                $this->Session->setFlash(__('City was not deleted',
true));
                $this->redirect(array('action' => 'index'));
        }
}

?>

//views/cities/obtener_provinciast.ctp
<?php
  if(isset($provinces)) {
    echo $this->Js->object($provinces);
  }
?>

//webroot/js/list_provinces.js
<?php
  if(isset($provinces)) {
    echo $this->Js->object($provinces);
  }
?>

//webroot/js/list_provinces.js
$(document).ready(function(){
    $('#country').live('change', function() {
        if($(this).val().length != 0) {
            $.getJSON('obtener_provincias',
            {
                countryId: $(this).val()
            },
            function(provinces) {
                if(provinces !== null) {
                    populateProvinceList(provinces);

                }

            });
        }
    });

});

function populateProvinceList(provinces) {
    var options = '';
    $.each(provinces, function(index, province) {
        options += '<option value="' + index + '">' + province + '</
option>';
    });
    $('#provincia').html(options);
    $('#provincias').show();

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to