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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:00:07+00:00 2026-05-15T17:00:07+00:00

I am trying to load values into a select dynamically using values from a

  • 0

I am trying to load values into a select dynamically using values from a json string.

The data is being loaded correctly, but for some reason, only the 1st value is loading into the select. Can anyone spot why this could be?

Here is my JSON data:

[{
  "cat_id": "1",
  "cat_section": "pages",
  "cat_type": "cat",
  "cat_name": "Music",
  "cat_order": "1",
  "cat_parent_id": "0"
}, {
  "cat_id": "2",
  "cat_section": "pages",
  "cat_type": "cat",
  "cat_name": "Arts & Culture",
  "cat_order": "2",
  "cat_parent_id": "0"
}, {
  "cat_id": "3",
  "cat_section": "pages",
  "cat_type": "cat",
  "cat_name": "Travel & Escape",
  "cat_order": "3",
  "cat_parent_id": "0"
}, {
  "cat_id": "4",
  "cat_section": "pages",
  "cat_type": "cat",
  "cat_name": "Technology",
  "cat_order": "4",
  "cat_parent_id": "0"
}]

An here is my jQuery:

$("select#select_category").change(function() {
  $.getJSON("<?php echo site_url()?>ajax/categories/pages/" + $(this).val(), function(data) {
    var options = '';
    for (var i = 0; i < data.length; data++) {
      options += '<option value="' + data[i].cat_id + '">' + data[i].cat_name + '</option>';
    }
    $("select#select_subcategory").html(options);
  })
});
  • 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-15T17:00:08+00:00Added an answer on May 15, 2026 at 5:00 pm
    for (var i = 0; i < data.length; data++) {
    

    Should clearly be i++.

    But in general you shouldn’t create dynamic page content using HTML string-slinging. Your code will fail when category names contain characters that are special in HTML markup. If categories may be input by the user, that gives you cross-site-scripting security holes.
    Instead you can use the jQuery 1.4 creation shortcut $('<option>', {text: foo, value: bar}), or even more simply in this case, new Option(). Either way sets properties directly instead of embedded in a string, so you don’t have to worry about HTML-escaping issues.

    Also when you spit strings (or other data) from PHP to JavaScript source, you should be using json_encode() to convert them properly, otherwise you’ll have problems with characters that are special in JavaScript string literals (', ", \ and various control codes) or the enclosing HTML file (</script>). Again, maybe this won’t matter for the specific values you have here, but in general this stuff is the source of many security-sensitive escaping problems.

    var siteurl= <?php echo json_encode(site_url(), JSON_HEX_TAG); ?>;
    
    $('#select_category').change(function(){
        $.getJSON(siteurl+'ajax/categories/pages/'+$(this).val(), function(data) {
            $('#select_subcategory').empty();
            $.each(data, function() {
                $('#select_subcategory').append(new Option(this.cat_name, this.cat_id));
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically load values into a select in a jqGrid. It almost
I am trying to load data from XML string into a structure of some
Trying to load a small .txt file into mysql but get all my data
I'm trying to load data from a .txt file with PHP, but I fail
I'm trying to load values into a SQLite table on android. My application keeps
trying to load in a bunch of images into a list from a directory...my
I have an Excel file that I am trying to load into R using
For the following statement: INSERT INTO main_app_provider (provider) VALUES ((SELECT distinct(provider) FROM raw_financials)); I
I'm trying to use the OleDb CSV parser to load some data from a
I'm trying to read data from an Excel document in C# using Microsofts COM

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.