开发者

php json jquery and select box

开发者 https://www.devze.com 2022-12-23 02:04 出处:网络
I have this php code $jsonArray = array(); $sql = \"SELECT ID,CLIENT FROM PLD_SERVERS\"; $result = mysql_query($sql);

I have this php code

$jsonArray = array();
$sql = "SELECT ID,CLIENT FROM PLD_SERVERS";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
    $jsonArray[] = array('id'=>$row['ID'],'开发者_如何学Pythonclient'=>$row['CLIENT']);
}
echo json_encode($jsonArray);

And this js

function autosearchLoadServers()
{

  $.post("php/autosearch-load-servers.php",function(data){
      var toAppend = "";
      for(var i = 0; i < data.length; i++){
          toAppend += '<option value = \"' + data[i].id + '\">' + data[i].client + '</option>';
      }
      $("#serverSelect").empty();
      $("#serverSelect").html(toAppend);
  });

}

The problem is that i get only undefined values. How can this be? The values are in the JSON, i checked using firebug in mozilla so there has to be something with the data variable but i can't understand what. I tried different ways and no results.


Try specifying the datatype in the post call like this:

 $.post("php/autosearch-load-servers.php",function(data){
  var toAppend = "";
  for(var i = 0; i < data.length; i++){
      toAppend += '<option value = \"' + data[i].id + '\">' + data[i].client + '</option>';
  }
  $("#serverSelect").empty();
  $("#serverSelect").html(toAppend);
}, "json");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号