0

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>
2
  • Why do you add an extra </option> before each <option>? Commented Jun 3, 2014 at 3:19
  • There are certain mistakes like extra </option> also in the dom element you are adding this in a div which is in a select, that's a bad practice although. Commented Jun 3, 2014 at 3:20

2 Answers 2

2

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>';
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks. That did not solve the problem. I have doubts if the <div id="ddlist"></div> will actually work inside <select id='EmpLst' name.....
Possibly true. Why don't you append to the <select> instead of the <div>?
I have thought about that, but I'm not sure how to get "SELECT STUDENT ID" as the 1st item and also to get onchange='document.getElementById("val1").value = this.value;'><option value="">
Since you're concatenating to the inner HTML, all that stuff will be left there.
0
ddlist.innerHTML += '</option><option value='+d[o].title+'</option>';  

should be

ddlist.innerHTML += '<option value="'+d[o].title+'">'+d[o].title+'</option>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.