Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从JSON填充选择列表_Php_Json_Jquery - Fatal编程技术网

Php 从JSON填充选择列表

Php 从JSON填充选择列表,php,json,jquery,Php,Json,Jquery,我一直在寻找一个看似简单的问题的解决方案,但无法找到解决方案…任何指导都将不胜感激 我试图使用从PHP脚本检索的JSON对象构建一个选择框。PHP脚本versions.PHP正在对数据库表运行查询;代码如下: $posts = array(); if(mssql_num_rows($result)) { while($post = mssql_fetch_assoc($result)) { $posts[] = $post; } } header('Content-type: a

我一直在寻找一个看似简单的问题的解决方案,但无法找到解决方案…任何指导都将不胜感激

我试图使用从PHP脚本检索的JSON对象构建一个选择框。PHP脚本versions.PHP正在对数据库表运行查询;代码如下:

$posts = array();
if(mssql_num_rows($result)) {
  while($post = mssql_fetch_assoc($result)) {
    $posts[] = $post;
  }
}
header('Content-type: application/json');
echo json_encode($posts);
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);
        });
    });
});
…并返回以下json结构:

[{版本:3.3.0},{版本:1.5.0}]

PHP文件是从一个中心JS文件调用的,该文件的结构如下:

$posts = array();
if(mssql_num_rows($result)) {
  while($post = mssql_fetch_assoc($result)) {
    $posts[] = $post;
  }
}
header('Content-type: application/json');
echo json_encode($posts);
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);
        });
    });
});
看看firebug,我看到的是对象列表,但不是数组中的值。我猜这与上面javascript文件中的代码有关——只是不确定如何从json对象访问值


提前谢谢。

你就快到了。根据您的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);
        });
    });
});