Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:29:32+00:00 2026-06-09T21:29:32+00:00

Hi all want to generate a dynamic drop down for Cities , as per

  • 0

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 :

  public function citylist() {
         $this->layout=false;
         Configure::write('debug', 0);
         if($this->request->is('ajax')){    
        $country=$this->Country->read(null, $this->request['url']['country_id']);
        $code=$country['Country']['country_code'];
        $cities=$this->Country->City->find('list',array('conditions' =>array('City.country_code' => $code),'recursive' => -1,'fields' => array('id', 'name')));
        $this->set('cities',$cities);
}}

and my jquery code is this:

$(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 += '<option value="' + index + '">' + city + '</option>';
});
$('#ProfileHomeLocation').html(options);
$('#city-list').show();
}

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].

I followed these two existing answers and it confused me further.
cakephp pass dropdown value in ajax
The connection was reset issue in CakePHP 2.2.

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

GET http://example.com/crush/countries/citylist?country_id=103 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

Don’t know what to do further.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:29:33+00:00Added an answer on June 9, 2026 at 9:29 pm

    In cases like yours, where a dynamic drop down box is involved, I use the ajax() jQuery’s method to retrieve the data and replace the whole content of the select element. So, instead of returning the JSON data through your AJAX call, instead you can return the full HTML option elements and replace the content of the select element with $('myselectelement').html(data). This works fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate a choicelist for all specs that inherit from imagekit.specs.ImageSpec .
I want to generate a classpath automatically with all the *.jar files contained under
I want to generate a heat map from an all-vs-all comparison. I have the
I want to generate a Css that will be the same for all my
Hi I want to delete all stuff related to rails generate scaffold at once.
Possible Duplicate: How to generate all permutations of a list in Python I want
I'm trying to build a new web app using Eclipse, I want to use
I want all my dynamic generated php links to be displayed like in example
The WSDL generated by WCF is describing all my bindings, and I want it
All I want to do is return the index of the i that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.