我正在运行一个简单的脚本来将mysql中的数据导入到选择框中,但它不会获取任何数据。下面是代码:
<body>
Team : <select id="teamname"> </select> <br />
Members : <select id="members"> </select>
</body>
Jquery:
$("document").ready(function(){
$.getJSON("ajax.php", function(data){
$("#teamname").empty();
$.each(data.result, function() {
$("#teamname").append("<option>" + this('team_name') + "</option>");
});
});
PHP
$result=array();
$getteams=mysql_query("select * from tbl_teams");
if(mysql_num_rows($getteams)){
while($gotteams=mysql_fetch_array($getteams)){
array_push($result,array('id' => $gotteams['team_id'],
'name' => $gotteams['team_name']));
}
echo json_encode(array('result' => $result));
}
我尝试了所有在线和其他在线可用的例子,但仍然无法正常工作。请帮帮我。
答案 0 :(得分:1)
您的列名为&#39; name&#39;不是&#39; team_name&#39;在数组中('name' => $gotteams['team_name']
)
尝试
$.each(data.result, function() {
$("#teamname").append("<option>" + this['name'] + "</option>");
});