0

I'm trying to populate a drop-down menu from a MySql.

My problem is that the data from JASON are not showing in my HTML page.

This is what I want to achieve. ID: Select ID

JASON //This works and the output: {"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>
4
  • 2
    Don't build your JSON manually. I would also think what you are looking for as output is an array structure enclosed by [] in JSON. Commented Jun 2, 2014 at 17:44
  • I suspect ddlist.innerHTML += '</option> isn't going to do what you think it does. (even though it likely will work anyway in most browsers) Commented Jun 2, 2014 at 17:45
  • 1
    you opened an xhr request, but you never sent it. Commented Jun 2, 2014 at 17:48
  • @KevinB thanks. that did not do the job. Commented Jun 2, 2014 at 17:57

1 Answer 1

2

try this

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

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.