Thursday, April 21, 2011

Re: return Json values cakephp 1.3.8

I have included the json_encode echo ($ provinces) and the action continues without returning kos edit json values​​. Add action in l if I return the values​​.

my controller


<?php

class CitiesController extends AppController {

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

    public function obtener_provincias() {
        if (!empty($this->data['City']['province_id'])){
              $this->data = "";
                 if ($this->RequestHandler->isAjax()) {
                $this->set('provinces', $this->City->Province->find('list',
                                array('conditions' =>
                                     array('province.country_id' => $this->params['url']['countryId']),
                                    'recursive' => -1)));
    echo json_encode($provinces);
            }
        }
                else{
            if ($this->RequestHandler->isAjax()) {
                $this->set('provinces', $this->City->Province->find('list',
                                array('conditions' =>
                                     array('province.country_id' => $this->params['url']['countryId']),
                                    'recursive' => -1)));
                                    echo json_encode($provinces);
            }
           
            }


    }

    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)) {
          //  pr($this->data);
            exit();

            $this->City->create();
            if ($this->City->save($this->data)) {
                $this->flashSuccess(__('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)) {
            pr($this->data);
            exit();
            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', array('conditions' =>
                    array('province.country_id' => ($this->data['Country']['id']))));
        $this->set(compact('countries', '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);

                }
                   
            });
        }
    });
      $('#province').live('change', function() {
           if($(this).val().length != 0) {
            $.getJSON('obtener_ciudades',
            {
                provinceId: $(this).val()
            },
            function(cities) {
                    if(cities !== null) {
                    populateCityList(cities);

                }
                   
            });
        }
    });
});

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

}
function populateCityList(cities) {
    var options = '';
    $.each(cities, function(index, city) {
        options += '<option value="' + index + '">' + city + '</option>';
    });
    $('#city').html(options);
    $('#cities').show();

}

my script



--
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+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: