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

  • SEARCH
  • Home
  • 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 8701039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:18:04+00:00 2026-06-13T02:18:04+00:00

I need to be able to select a country in a selectbox, and then

  • 0

I need to be able to select a country in a selectbox, and then get all the states from that country.

I’m trying to do something like this: how-to-display-json-data-in-a-select-box-using-jquery

This is my controller:

foreach($this->settings_model->get_state_list() as $state)
{
   echo json_encode(array($state->CODSTA, $state->STANAM));
}

and my javascript:

 $.ajax({
    url: 'settings/express_locale',
    type: 'POST',
    data: { code: location, type: typeLoc },
    success: function (data) {
        console.log(data);
    }
});

console.log shows me something like this:

["71","SomeState0"]["72","SomeState"]["73","SomeState2"]["74","SomeState3"]

So, what i need is to append all states in a new selectbox.

But I’m trying to read this array doing this in the success callback:

$.each(data, function(key,val){
   console.log(val);
});

In the result, each line is a word, like this:

[
 "
 7
 1
 "
 ,
 "
 s
 ....
 ]

Why does that happen, and what am I missing?

  • 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-13T02:18:05+00:00Added an answer on June 13, 2026 at 2:18 am

    JSON is not made of independent blocks. So this will never do:

    foreach($this->settings_model->get_state_list() as $state)
    {
        echo json_encode(array($state->CODSTA, $state->STANAM));
    }
    

    The output will be treated as text, and the iterator will loop the object’s elements… which are the single characters.

    You need to declare a list, or a dictionary. I have included some examples, depending on how you use the data in the jQuery callback. Note: PHP-side, you may also need to output the proper MIME type for JSON:

    $states = array();
    foreach($this->settings_model->get_state_list() as $state)
    {
        // Option 1: { "71": "SomeState0", "72": "Somestate2", ... }
        // Simple dictionary, and the easiest way IMHO
        $states[$state->CODSTA] = $state->STANAM;
    
        // Option 2: [ [ "71", "SomeState0" ], [ "72", "SomeState2" ], ... ]
        // List of tuples (well, actually 2-lists)
        // $states[] = array($state->CODSTA, $state->STANAM);
    
        // Option 3: [ { "71": "SomeState0" }, { "72": "SomeState2" }, ... ]
        // List of dictionaries
        // $states[] = array($state->CODSTA => $state->STANAM);
    }
    
    Header('Content-Type: application/json');
    // "die" to be sure we're not outputting anything afterwards
    die(json_encode($states));
    

    In the jQuery callback, you specify the datatype and content type with charset (this will come in handy as soon as you encounter a state such as the Åland Islands, where a server sending data in ISO-8859-15 and a browser running a page in UTF8 can lead to a painful WTF moment):

            $.ajax({
                url: 'settings/express_locale',
                type: 'POST',
                data: { code: location, type: typeLoc },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    $("#comboId").get(0).options.length = 0;
                    $("#comboId").get(0).options[0] = new Option("-- State --", "");
                    // This expects data in "option 1" format, a dictionary.
                    $.each(data, function (codsta, stanam){
                       n = $("#comboId").get(0).options.length;
                       $("#comboId").get(0).options[n] = new Option(codsta, stanam);
                 });
               },
               error: function () {
                    alert("Something went wrong");
               }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to select all columns from two tables, but need to be able
I need to be able to select an element that has certain content (innerHTML).
I need to be able to select a certain range of rows from a
I have a mysql table that looks something like this: Row 1: 'visitor_input_id' =>
i need to be able to select Xpaths in jquery. selecting via CSS is
I need to be able to select multiple options on a web form, but
I need to have a DateTimePicker which will be able to select the Time
public function dcr() { $query = $this->db->query(SELECT * FROM church_repo); foreach ($query->result() as $row)
I have the jQuery datepicker working, but I need to be able to select
I am currently writing an application that needs to be able to select a

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.