1

I read example in http://www.w3schools.com/json/json_example.asp.

And I try it with my MySQL database and my PHP code. But it does not work. I am a beginner and I don't know what it wrong.

Here is my PHP link: http://xebus2014.tk/demo.php

And I change w3school code like this:

<!DOCTYPE html>
<html>

<head>
<style>
h1 {
    border-bottom: 3px solid #cc9900;
    color: #996600;
    font-size: 30px;
}
table, th , td  {
    border: 1px solid grey;
    border-collapse: collapse;
    padding: 5px;
}
table tr:nth-child(odd)	{
    background-color: #f1f1f1;
}
table tr:nth-child(even) {
    background-color: #ffffff;
}
</style>
</head>

<body>

<h1>Customers</h1>
<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://xebus2014.tk/demo.php";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var arr = JSON.parse(response);
    var i;
    var out = "<table>";

    for(i = 0; i < arr.length; i++) {
        out += "<tr><td>" + 
        arr[i].STT +
        "</td><td>" +
        arr[i].ID +
        "</td><td>" +
        arr[i].Name +
        "</td><td>" +
        arr[i].Singer +
        "</td></tr>";
    }
    out += "</table>"
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>
PHP code

14
  • Also post your php code. Don't put the php in a snippet (as it won't work) Commented Jun 8, 2015 at 8:37
  • what is your error message Commented Jun 8, 2015 at 8:38
  • You don't need to parse the response as it's already JSON. This is probably the error. Commented Jun 8, 2015 at 8:40
  • I executed the code and it worked correctly. I have used PHP as below <?php echo '[{"STT":"1","ID":"123","Name":"Sexy Love","Singer":"T-ara"},{"STT":"2","ID":"456","Name":"Day By Day","Singer":"T-ara"},{"STT":"3","ID":"789","Name":"Cry Cry","Singer":"T-ara"}]'; ?> Commented Jun 8, 2015 at 8:40
  • Your code is fine, but you may encounter cross domain issues if the page does not come from the same domain xebus2014.tk unless you implemented CORS Commented Jun 8, 2015 at 8:42

2 Answers 2

1

Did you set the response as Json String?

header("Content-Type: application/json; charset=UTF-8");
Sign up to request clarification or add additional context in comments.

1 Comment

I add header in my php code. But it still dose not display data.
1

Your result is already an object, so you don't need to parse it using JSON.parse, just use it as is and pass it through the loop as is.

I don't know about the cross domain issue, but to run it, I go directly to your function... see this fiddle for a working loop using your output -- http://jsfiddle.net/fu3g5Loe/

OR you may use this in your PHP backend

echo json_encode('Your result set goes here'); // this will apply the correct JSON representation of your result set before echoing

Working Code

<body>

<h1>Customers</h1>
<div id="id01"></div>

<script>
    /*
    var xmlhttp = new XMLHttpRequest();
    var url = "http://xebus2014.tk/demo.php";

    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
    xmlhttp.send();
    */

    var params = [{"STT":"1","ID":"123","Name":"Sexy Love","Singer":"T-ara"},{"STT":"2","ID":"456","Name":"Day By Day","Singer":"T-ara"},{"STT":"3","ID":"789","Name":"Cry Cry","Singer":"T-ara"}];

    myFunction(params);

function myFunction(response) {
    var arr = response;

    var i;
    var out = "<table>";

    for(i = 0; i < response.length; i++) {  
    out += "<tr><td>" + 
        arr[i].STT +
        "</td><td>" +
        arr[i].ID +
        "</td><td>" +
        arr[i].Name +
        "</td><td>" +
        arr[i].Singer +
    "</td></tr>";
    }
    out += "</table>"
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>

2 Comments

I want to read database from server. Console response me " Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?" @Dexter Huinda
just try to replace the line that says var arr = JSON.parse(response) with var arr = response and test again.

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.