php - Highcharts failing to display data -
i trying create simple program takes weather data database have setup, , display on webpage. way have setup right have enter value in text box in order data pull.
my html code:
<!doctype html> <html> <head> <title>database</title> </head> <body> name: <input type="text" id="name"> <input type="submit" id="name-submit" value="grab"> <div id="name-data"></div> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> <script src="http://code.jquery.com/jquery-1.8.0.min.js"> </script> <script src="js/global.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"> </script> </body> </html>
my php code:
<?php if(isset($_post['name']) === true && empty($_post['name']) === false) { mysql_connect("localhost", "root", "logilube163"); mysql_select_db("weather_test_simple_schema__rev1"); $query = mysql_query("select * location"); while($row = mysql_fetch_array($query)){ $time = strtotime($row['time']) * 1000; $tempf = $row['temp_f']; ![enter image description here][1]$data []= [(int)$time, (int)$tempf]; } echo json_encode($data); } ?>
the code able query database, , put data data array $data.
the array of data encoded using json_encode(); function , transferred javascript file being read out , placed in on html page.
my js code:
$('input#name-submit').on('click', function(){ var name = $('input#name').val(); if($.trim(name)!=''){ $.post('/ajaxdb/ajax/name.php',{name: name}, function(data){ $('div#name-data').text(data); // outputs data before graph $('#container').highcharts({ chart: { zoomtype: 'x' }, xaxis: { type: 'datetime', minrange: 24 * 3600000 }, title : { text : 'weather data' }, series : [{ data: data // <-- data not being read in graph //[[1432813308000,78],[1432813904000,77],[1432813968000,77],[1432814016000,77],[1432815600000,78],[1432832464000,66],[1432843057000,65],[1432843731000,65]] //the above data read in graph , plots fine. }] }); }); } });
my output result looks this:
the problem same data read out in javascript, , not being able placed in highcharts graph. instead, chart gets created correct axis without data points. however, when enter data points being created tag, data gets plotted fine.
to answer own question, found needed code:
header("content-type: text/json");
at top of .php file.
Comments
Post a Comment