Can someone help me figure out why the json array I have is not population my select drop down? The array prints fine (name:value) and I have tried different ways (.post .getJSON) to fill the select fields. See my code below…
PHP
$q = "select Site_ID, FirstName, LastName from ClientInfo";
$sql = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($sql, true)){
$data[] = $row;
};
echo json_encode($data);
This is what it prints on webpage:
[{"Site_ID":"10000001","FirstName":"drew","LastName":"0"}, {"Site_ID":"10000002","FirstName":"hello","LastName":"0"},{"Site_ID":"10000003","FirstName":"hihi","LastName":"0"},{"Site_ID":"10000004","FirstName":"","LastName":"0"},{"Site_ID":"10000005","FirstName":"SueAnn","LastName":"Hall"},{"Site_ID":"10000006","FirstName":"Test","LastName":"Name"}]
HTML with script
<body>
<script type="text/javascript">
$(function(){
$.getJSON('checkin.php',function(data){
$.each(data,function(name,value)
{
items+="<option value='"+item.id+"'>"+item.name+"</option>";
});
$("#clients").html(items);
});
});
</script>
<select id="clients">
<option>Default</option>
</select>
</body>
Works fine until I run the html. Thank you in advance for your help.
itemsvariable before concatenation.itemin your code isundefined, you should usevalueproperty that you have passed as the argument to the handler.Your objects doesn’t have
idandnameproperties.http://jsfiddle.net/5PEPg/