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 469113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:44:59+00:00 2026-05-12T23:44:59+00:00

I am trying to populate a select box (cities related to a state) using

  • 0

I am trying to populate a select box (cities related to a state) using the $.ajax method of jquery.

I wrote the following in my php script

    $('#cmbState').change(function(){
  $('#cmbCity').children().remove();
  $.ajax({
   type: "POST",
   url: "../include/ajax.php",
   data: "option=getCitiesList&stateid="+$(this).val()+"",
   dataType: "json",
   complete: function(response){
     'what should I write in here ..?'
     },
   beforeSend: function(){$('#cmbCity').addClass('show_loading_in_center')},
   success: function(){$('#cmbCity').removeClass('show_loading_in_center')}
     });
    });

and in the file ‘ajax.php’ (file where the query is sent for getting the realted cities) I did

$i=0;
foreach($cities as $city)
 {
  $response[$i]['id'] = $city['pk_cityid'];
  $response[$i]['name'] = $city['name'];
  $i++;
 }
 echo json_encode($response);

Now the response comes as XMLHTTPResponse object.
How shall I populate the response into cities selectbox.?

Please help, I am really stuck over here.

Thanks

  • 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-05-12T23:45:00+00:00Added an answer on May 12, 2026 at 11:45 pm
    $('#cmbState').change(function () {
        $.ajax({
            type: "POST",
            url: "../include/ajax.php",
            data: {option: "getCitiesList", stateid: $(this).val()},
            dataType: "json",
    
            success: function (data) {
                var $cmbCity = $('#cmbCity').empty();
                $.each(data, function () {
                     $cmbCity.append('<option value="' + this.id + '">' + this.name + '</option>')
                });
            },
    
            beforeSend: function(){ $('#cmbCity').addClass('show_loading_in_center') },
            complete: function(){ $('#cmbCity').removeClass('show_loading_in_center') }
        });
    });
    

    You need to write a success handler that will be called with the data passed (completed is called even on error). The data is passed as the first argument. We first empty() the select and assign it to $cmbCity for performance reasons. Then we iterate over data using $.each (cross-browser jQuery iteration) that binds each of the elements to this. Using that we create new options in the #cmbCity select.

    The above code assumes that the response is in the following format:

    [{"id": 1, "name": "London"}, 
     {"id": 2, "name": "Paris"},
     {"id": 3, "name": "New York"}]
    

    Also, note that data (in the $.ajax call above) is in key-value format – jQuery will serialize it automatically.

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

Sidebar

Related Questions

I am trying to create a drop down list using jQuery, PHP and mySQL,
I am trying to populate an HTML select list using an array of customer
I am trying to populate the list of an <h:selectOneMenu> . However, the list
I'm trying to populate a second dropdown in a dynamic way based on a
I am trying to populate a tree view based on a list of staff
I am trying to send a variable with jQuery's getJSON function but it doesn't
I have a combo box and several text boxes on a form. When I
I have a Contact Domain class that can be associated with multiple Organizations, which
Still really struggling with this and appear to be going round in circles. I
I have defined three tables, Stores, InventoryItems and StoreItemRecords. My StoreItemRecords table has foreign

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.