How to properly unpack data from PHP array encoded as JSON from a JavaScript file

时间:2015-07-29 00:13:52

标签: javascript php ajax json chart.js

I have been doing searches for hours upon hours and have tried different snippets of codes and techniques and I simply cannot get any of them to work. I have done research, I am simply a very inexperienced developer trying to learn as I go.

My ultimate goal: Have a live-updating graph of test results data pulled from a MySQL database and rendered using Chart.JS library.

What I have done so far:

  1. I have an HTML file with embedded JavaScript and calls to AJAX function to auto-refresh page every 5 minutes (this has been tested and works).
  2. I have a PHP file which opens a PDO connection to a local MySQL database, runs 3 selection queries, and stores the results into 3 PHP arrays. (I have iterated through the arrays and echoed the results and the data is good).
  3. I have then stored the data inside these PHP arrays into a JSON object using json_encode() e.g. $(".<?php echo $id ?>").click(function(e) { ....

I read that one method to consume this JSON data is to use jQuery's $.getJSON method, and in order to use it, I must 'echo' out the results of my json_encoded object. I realize I am printing the results twice. The echo $json_totalTestsPassedArray is for the aforementioned purpose.

  1. I have included the path of this PHP file in my HTML file from #1 using $totalTestsPassedArray = array(); $sql = "SELECT totalTestsPassed FROM execution ORDER BY lastDate"; $query = $conn->prepare($sql); $query->execute(array(':totalTestsPassed' => $totalTestsPassed)); while($row = $query->fetch()){ array_push($totalTestsPassedArray, $row['totalTestsPassed']); } $json_totalTestsPassedArray = json_encode($totalTestsPassedArray); //pack data into JSON object echo $json_totalTestsPassedArray; // for accessibility by AJAX foreach ($totalTestsPassedArray as $item){ //print out contents of array echo $item . '<br>'; }
  2. I have tried several ways of unpacking this JSON encoded object packed with my database data: using AJAX requests, making a new array in the HTML and then flooding it with the JSON object using $.getJSON, and some other things people have suggested to do on various documents online, all met with failure.

Could someone please tell me the proper way to do what I am trying to do? I have a PHP file with an array of data, and I would like to pass that array of data into a dataset for use by Chart.JS to populate a graph and then draw it to the canvas and display my results.

<?php require '../file_name.php';?>

Again, please forgive my ignorance. I have tried my best to figure out the solution on my own and would really appreciate some guidance. It's my first time using these tools. I thought it would be as simple as:

  • Query DB using PHP script
  • Store results into array
  • Pack into JSON object (the goal being to transfer data from PHP to JS file)
  • Unpack JSON object in JavaScript
  • Load unpacked data into my JavaScript array

1 个答案:

答案 0 :(得分:0)

仅在php打印json并将标题content type设置为application/json,然后您的XHR响应将包含您的数据的javascript对象。不需要任何json解析。