title: dynamic drop down with cakephp and Jquery
body: <p>Hi all want to generate a dynamic drop down for Cities , as per the selection of country in cakephp 2.2 app. I am quite new to cakephp.Moreover i hardly find any tutorial for they are mostly for 1.3 or 1.2 version. I am having Drop down menus for country and city in Profile add view. Here is my code in the countries Controller though :</p>
  
  <pre><code>  public function citylist() {
           $this-&gt;layout=false;
           Configure::write('debug', 0);
           if($this-&gt;request-&gt;is('ajax')){    
          $country=$this-&gt;Country-&gt;read(null, $this-&gt;request['url']['country_id']);
          $code=$country['Country']['country_code'];
          $cities=$this-&gt;Country-&gt;City-&gt;find('list',array('conditions' =&gt;array('City.country_code' =&gt; $code),'recursive' =&gt; -1,'fields' =&gt; array('id', 'name')));
          $this-&gt;set('cities',$cities);
  }}
  </code></pre>
  
  <p>and my jquery code is this:</p>
  
  <pre><code>$(document).ready(function(){
  $('#ProfileCountryId').live('change', function() {
  if($(this).val().length != 0) {
    $.getJSON('/crush/countries/citylist',
                {country_id: $(this).val()},
                function(cities) {
                  if(cities !== null) {
                    populateCityList(cities);
                  }
      });
    }
  });
  });
  
        function populateCityList(cities) {
  var options = '';
  
  $.each(cities, function(index, city) {
  options += '&lt;option value="' + index + '"&gt;' + city + '&lt;/option&gt;';
  });
  $('#ProfileHomeLocation').html(options);
  $('#city-list').show();
  }
  </code></pre>
  
  <p>I tried making changes but it really confusing as in diff examples i see very diff ways. 
  I am not sure wether to check that request type as ajax or should i fetch the parameter value through  request->data or params[url].</p>
  
  <p>I followed these two existing answers and it confused me further.
  <a href="https://stackoverflow.com/questions/9498560/cakephp-pass-dropdown-value-in-ajax/12004026#12004026">cakephp pass dropdown value in ajax</a>
  <a href="https://stackoverflow.com/questions/11520404/the-connection-was-reset-issue-in-cakephp-2-2">The connection was reset issue in CakePHP 2.2</a>.</p>
  
  <p>The error is strange that first time page is loaded it shows error in console "Failed to load resource: the server responded with a status of 500 (Internal Server Error)".
  However if i load reload and then select the first value in the drop down. It gives error</p>
  
  <blockquote>
    <p>GET <a href="http://example.com/crush/countries/citylist?country_id=103" rel="nofollow noreferrer">http://example.com/crush/countries/citylist?country_id=103</a> 500
    (Internal Server Error) jQuery.ajaxTransport.sendjquery-1.7.2.js:8240
    jQuery.extend.ajaxjquery-1.7.2.js:7719 jQuery.each.jQuery.(anonymous
    function)jquery-1.7.2.js:7245
    jQuery.extend.getJSONjquery-1.7.2.js:7262 (anonymous
    function)citylist.js:4 jQuery.event.dispatchjquery-1.7.2.js:3332
    jQuery.event.add.elemData.handle.eventHandle</p>
  </blockquote>
  
  <p>Don't know what to do further.</p>