MySQL的 - JSON数据没有显示在HTML [英] MySql - JSON data not showing in html

查看:469
本文介绍了MySQL的 - JSON数据没有显示在HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个下拉列表,从一个MySQL。

该PHP是获取成功从MySQL中的数据。但我的问题是数据不显示在我的HTML页面的下拉列表?

json_mysql_data2.php

 标题(内容类型:应用程序/ JSON);
require_once(con.php);
$ i = 0;
$ jsonData =阵列();
的foreach($ conn_db->查询(选择从的customerID客户WHERE家具= '33')为$结果){
        $ I ++;
        $ jsonData [文章$ I] = $结果['的customerID'];
    }
回声json_en code($ jsonData);
 

myJS.js

  $(文件)。就绪(函数(){
    变种ddlist =的document.getElementById(ddlist);
    VAR小时=新XMLHtt prequest();
    hr.open(GET,json_mysql_data2.php,真正的);
    hr.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
    hr.onreadystatechange =功能(){
        如果(hr.readyState == 4和&安培; hr.status == 200){
            变种D = JSON.parse(hr.responseText);
            为(在D变种O){
                如果(D [O] .title伪){
                    ddlist.innerHTML + ='< /选项><期权价值='+ D [O] .title伪+'< /选项>';
                }
            }
        }
    }
    hr.send(空);
    ddlist.innerHTML =装入客户ID ......;
});
 

HTML

 <脚本SRC =myJS.js类型=文/ JavaScript的>< / SCRIPT>
< /头>
<身体GT;
< D​​IV CLASS =DLIST>
        客户ID:
        <选择一个id ='EmpLstNAME =dwlist的onchange ='的document.getElementById(VAL1)值= THIS.VALUE。><期权价值=>选择学生证和LT; /选项>
        < D​​IV ID =ddlist>< / DIV>
        < /选择>
        < / DIV>
 

解决方案

的innerHTML 是错误的。你有一个额外的< /选项> 一开始,你就错过了结束> <选项> 。你也不必在选择任何文本,所以菜单项都将是空白的。

您还应该追加到<选择> ,而不是< D​​IV>

  hr.onreadystatechange =功能(){
    如果(hr.readyState == 4和&安培; hr.status == 200){
        变种D = JSON.parse(hr.responseText);
        变种empList =的document.getElementById(EmpList);
        为(在D变种O){
            如果(D [O] .title伪){
                empList.innerHTML + ='<期权价值='+ D [O] .title伪+'>'+ D [O] .title伪+'< /选项>';
            }
        }
    }
}
 

I'm trying to create a drop down list from a MySql.

The php is successfully fetching the data from the MySql. But my problem is the data is not showing on the drop down list in my HTML page?

json_mysql_data2.php

header("Content-Type: application/json");
require_once("con.php");
$i=0;
$jsonData = array();
foreach ($conn_db->query("SELECT customerID FROM customers WHERE furniture='33' ") as $result){
        $i++;
        $jsonData["article".$i]=$result['customerID'];
    }
echo json_encode($jsonData);

myJS.js

$(document).ready(function(){
    var ddlist = document.getElementById("ddlist");
    var hr = new XMLHttpRequest();
    hr.open("GET", "json_mysql_data2.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>';
                }
            }
        }
    }
    hr.send("null");
    ddlist.innerHTML = "Loading Customer ID....";
});

html

<script src="myJS.js" type="text/javascript"></script>
</head>
<body>
<div class="dlist">
        Customer ID: 
        <select id='EmpLst' name="dwlist" onchange='document.getElementById("val1").value = this.value;'><option value="">SELECT STUDENT ID</option>
        <div id="ddlist"></div>
        </select>
        </div>

解决方案

Your innerHTML is wrong. You have an extra </option> at the beginning, you're missing the closing > of the <option>. You also don't have any text in the option, so the menu items will all be blank.

You also should be appending to the <select> instead of a <div>.

hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var d = JSON.parse(hr.responseText);
        var empList = document.getElementById("EmpList");
        for(var o in d){
            if(d[o].title){
                empList.innerHTML += '<option value="'+d[o].title+'">'+d[o].title+'</option>';
            }
        }
    }
}

这篇关于MySQL的 - JSON数据没有显示在HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆