我试图从MySql填充下拉菜单。
我的问题是来自JASON的数据没有显示在我的HTML页面中。
这就是我想要实现的目标。 ID:选择ID
JASON //这是有效的和输出:{" article1":{" title":" acGH2867" }," article2":{" title":" apGS0158" }}
$jsonData = '{';
foreach ($conn_db->query("SELECT customerID FROM customers WHERE furniture='33' ") as $result){
$i++;
$jsonData .= '"article'.$i.'":{ "title":"'.$result['customerID'].'" },';
}
$jsonData = chop($jsonData, ",");
$jsonData .= '}';
echo $jsonData;
HTML
<script type="text/javascript">
$(document).ready(function(){
var ddlist = document.getElementById("ddlist");
var hr = new XMLHttpRequest();
hr.open("cData.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var d = JSON.parse(hr.responseText);
for(var o in d){
if(d[o].title){
ddlist.innerHTML += '</option><option value='+d[o].title+'</option>';
}
}
}
}
ddlist.innerHTML = "Loading....";
$('#dlist').on('change', function(){
$('#val1').value() = $(this).val();
});
});
</script>
</head>
<div class="dlist" id="ddlist">
</div>
答案 0 :(得分:2)
试试这个
$jsonData = array();
foreach ($conn_db->query("SELECT customerID as title FROM customers WHERE furniture='33' ") as $result)
{
$i++;
$jsonData["article'.$i]=$result;
}
echo json_encode($jsonData);