Set picklist from JSON

I was looking for a solution for what seems like a simple problem, but I cannot find a solution ... any guidance would be appreciated.

I am trying to create a select box using a JSON object extracted from a PHP script. PHP script (version.php) performs a query on the database table; The code is as follows:

$posts = array();
if(mssql_num_rows($result)) {
  while($post = mssql_fetch_assoc($result)) {
    $posts[] = $post;
  }
}
header('Content-type: application/json');
echo json_encode($posts);

... and returns the following json structure:

[{"version": "3.3.0"}, {"version": "1.5.0"}]

The PHP file is called from a central JS file, which is structured as follows:

jQuery(function($){
    $.getJSON('versions.php', function(data) {
        var select = $('#release-list');
        $.each(data, function(key, val){
            var option = $('<option/>');
            option.attr('value', val)
                  .html(data)
                  .appendTo(select);
        });
    });
});

Looking at firebug, I see lists of objects, but not values ​​in the array. I think this is due to the code in the javascript file above - just not sure how to access the values ​​from the json object.

Thanks in advance.

+3
source
1

. JSON, :

jQuery(function($){
    $.getJSON('versions.php', function(data) {
        var select = $('#release-list');
        $.each(data, function(key, val){
            $('<option/>').attr('value', val.version)
                  .html('version ' + val.version)
                  .appendTo(select);
        });
    });
});
+4

All Articles