php json jquery and select box

Posted by user253530 on Stack Overflow See other posts from Stack Overflow or by user253530
Published on 2010-03-15T00:30:37Z Indexed on 2010/03/15 0:39 UTC
Read the original article Hit count: 344

Filed under:
|
|
|

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'],'client'=>$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.

© Stack Overflow or respective owner

Related posts about JSON

Related posts about jQuery