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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:22:42+00:00 2026-05-20T09:22:42+00:00

I have been going through this tutorial on auto-populating boxes using jQuery and Ajax:

  • 0

I have been going through this tutorial on auto-populating boxes using jQuery and Ajax:
http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/

and in the demo the author is running jQuery version 1.2.3. Locally I managed to get the function running on jQuery 1.3.2. but running it on any version above that one is not working (the second box is not populating).

Here is the jQuery code:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
  $("select#ctlJob").change(function(){
    $.getJSON("/select.php",{id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      $("select#ctlPerson").html(options);
    })
  })
})
</script>

This is the HTML code:

<form action="/select_demo.php">
  <label for="ctlJob">Job Function:</label>
  <select name="id" id="ctlJob">
    <option value="1">Managers</option>
    <option value="2">Team Leaders</option>
    <option value="3">Developers</option>
  </select>
  <noscript>
    <input type="submit" name="action" value="Load Individuals" />
  </noscript>
  <label for="ctlPerson">Individual:</label>
  <select name="person_id" id="ctlPerson">
    <option value="1">Mark P</option>
    <option value="2">Andy Y</option>
    <option value="3">Richard B</option>
  </select>
<input type="submit" name="action" value="Book" />
</form>

This is the server-side PHP:

<?php
if ($_GET['id'] == 1) {
  echo <<<HERE_DOC
[ {optionValue: 0, optionDisplay: 'Mark'}, {optionValue:1, optionDisplay: 'Andy'}, {optionValue:2, optionDisplay: 'Richard'}]
HERE_DOC;
} else if ($_GET['id'] == 2) {
  echo <<<HERE_DOC
[{optionValue:10, optionDisplay: 'Remy'}, {optionValue:11, optionDisplay: 'Arif'}, {optionValue:12, optionDisplay: 'JC'}]
HERE_DOC;
} else if ($_GET['id'] == 3) {
  echo <<<HERE_DOC
[{optionValue:20, optionDisplay: 'Aidan'}, {optionValue:21, optionDisplay:'Russell'}]
HERE_DOC;
}?>

How do I rewrite this function so it works with, for example, jQuery 1.5?

Thanks for the help!

EDIT: Mark’s solution worked, this is the HTML file with everything in it, and it should be relatively easy to adapt it to read a saved json file.

<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Select test</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
                 $(function(){
                    var data = [
                                [
                                    {optionValue: 0,optionDisplay: 'Mark'},
                                    {optionValue: 1,optionDisplay: 'Andy'},
                                    {optionValue: 2,optionDisplay: 'Richard'}
                                    ],
                                [
                                    {optionValue: 10,optionDisplay: 'Remy'},
                                    {optionValue: 11,optionDisplay: 'Arif'},
                                    {optionValue: 12, optionDisplay: 'JC'}
                               ],
                                [
                                    {optionValue: 20,optionDisplay: 'Aidan'},
                                    {optionValue: 21,optionDisplay: 'Russell'}
                                   ]
                               ];


                    $("#ctlJob").change(function() {
                        var $persons = $("#ctlPerson").empty();
                        $.each(data[$(this).val() - 1], function() {
                            $persons.append("<option value=" + this.optionValue + ">" + this.optionDisplay + "</option>");
                        });
                    });
                })    
                </script>
    </head>

    <body>
            <form action="/select_demo.php">
                <label for="ctlJob">Job Function:</label>
                <select name="id" id="ctlJob">
                    <option value="1">Managers</option>
                    <option value="2">Team Leaders</option>
                    <option value="3">Developers</option>
                </select>
                <noscript>
                    <input type="submit" name="action" value="Load Individuals" />
                </noscript>
                <label for="ctlPerson">Individual:</label>
                <select name="person_id" id="ctlPerson">
                    <option value="1">Mark P</option>
                    <option value="2">Andy Y</option>
                    <option value="3">Richard B</option>
                </select>
                <input type="submit" name="action" value="Book" />
            </form>
    </body>
</html>
  • 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-20T09:22:42+00:00Added an answer on May 20, 2026 at 9:22 am

    Assuming your json is valid you should be able to use the following:

     $("select#ctlJob").change(function(){
        $.getJSON("/select.php",{id: $(this).val(), ajax: 'true'}, function(data){
          var $persons = $("#ctlPerson").empty();
          $.each(data, function() {
            $persons.append("<option value=" + this.optionValue + ">" + this.optionDisplay + "</option>");
          });
        })
      });
    

    Updated to use your markup in the question on jsfiddle.

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

Sidebar

Related Questions

I have been tasked with going through a number of ColdFusion sites that have
I have been using C# for a while now, and going back to C++
I have been going back and forth between C# and Java for the last
I've switched over to a Mac recently and, although things have been going quite
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been experimenting with woopra.com A web analytics tool. Which requires a piece
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
Have been looking at the MVC storefront and see that IQueryable is returned from
Have been studying the file system related classes of Adobe AIR 1.5, but so
I have been working on a web services related project for about the last

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.