我想在 Highchart.js 图形中显示从 MySQL 收到的数据。为此,我使用了两个输入到日期信息的文本框。最后,使用一个调用函数的按钮(chart.php)。顺便说一下,我获取了日期信息,并在函数中使用 $ .posst 方法发送到 mysql.php 。然后我想让MySQL数据显示在 Highchart.js 中。但程序给我空阵。实际上,数组有很多数据,因为函数(数据)在($ .posd)中显示数据。
chart.php
function zaman_al()
{
var baslangic=document.getElementById('rest_example_3').value;
var son=document.getElementById('rest_example_4').value;
if (baslangic=="" || son=="")
{
alert("Lütfen Tarih bilgisi Giriniz !!!");
}
else
{
baslangic = new Date(baslangic);
baslangic = baslangic.getTime();
son = new Date(son);
son= son.getTime();
if (son<=baslangic)
{
alert("Geçersiz Tarih Girişi !!!");
}
if (son>baslangic)
{
var veri={'baslama':(baslangic/1000),'bitirme':(son/1000)};
$.post('mysql.php',veri,
function(data)
{
alert(data);
$(function () {
$('#kku').highcharts({
chart: {
type: 'spline'
},
title: {
text: 'Snow depth at Vikjafjellet, Norway'
},
subtitle: {
text: 'Irregular time data in Highcharts JS'
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { // don't display the dummy year
month: '%e. %b',
year: '%b'
},
title: {
text: 'Date'
}
},
yAxis: {
title: {
text: 'Snow depth (m)'
},
min: 0
},
tooltip: {
headerFormat: '<b>{series.name}</b><br>',
pointFormat: '{point.x:%e. %b}: {point.y:.2f} m'
},
plotOptions: {
spline: {
marker: {
enabled: true
}
}
},
series: [{
name: 'Winter 2007-2008',
// Define the data points. All series have a dummy year
// of 1970/71 in order to be compared on the same x axis. Note
// that in JavaScript, months start at 0 for January, 1 for February etc.
data: [
<?php
$veri=explode('"',$sonuc);
for ($i = 0; $i < count($veri); $i++)
{
if(($i%4)==1)
{
echo "[".$veri[$i].","; // time
}
if(($i%4)==3)
{
echo $veri[$i]."],\n"; // sensor_value
}
};
?>
]
}]
});
});
});
}
}
}
上面的按钮功能代码。顺便说一句。
chart.php
<b>Başlangıç Zamanı :</b> </td>
<td><input style="border-radius:15px" type="textbox" name="rest_example_3" id="rest_example_3" value="" /></td>
<td ><br><br><br></td>
</tr>
<tr>
<td><b>Bitiş Zamanı :</b> </td>
<td><input style="border-radius:15px" type="textbox" name="rest_example_4" id="rest_example_4" value="" /></td>
<td ><br><br><br></td>
</tr>
<tr>
<td ><br><br><br></td>
<td><input type="button" style="border-radius:15px; background-color:lightgreen; float:right" width="100" value="Get Chart" onClick="zaman_al()"> </td>
<td ><br><br><br></td>
</tr>
</table></center>
<div id="kku"></div>
当我发送($。POST)中的数据 mysql.php 时, mysql.php 正在给我通知:未定义的xxx 。
顺便说一下, mysql.php 如下:
<?php
$baslangic =$_POST['baslama']; // -> notice: Undefined index: baslama
$son=$_POST['bitirme']; // -> notice: Undefined index: bitirme
//$baslangic=1445780257; -> correct result for it
//$son=1445780290; -> correct result for it
//echo $son;
//echo $baslangic;
//settype($son, "int");
//settype($baslangic, "int");
//echo gettype(1445780290);
//echo gettype($son);
$baglanti= new mysqli("localhost","root","","deneme_kou");
$sql = "SELECT zaman,sensor_deger FROM sensor WHERE zaman>='$baslangic' AND zaman<='$son'";
$res=mysqli_query($baglanti,$sql);
$result = array();
while( $row = mysqli_fetch_array($res))
{
$result[]=array($row["zaman"],$row["sensor_deger"]);
}
$sonuc=json_encode(array($result));
echo json_encode(array($result));
?>
当我写 $baslangic=1445780257 $son=1445780290
; 而不是 $baslangic = $_POST['baslama']; $son=$_POST['bitirme']
时,我可以得到正确的结果并显示在 highchart.js
中