使用highcharts传递$ _POST [" City"] mysql

时间:2014-09-15 09:32:44

标签: php mysql highcharts

我尝试生成高图图形。如果我选择一个城市并将其作为$ _POST [“City”]传递,那么当它咨询我的基础mysql时,它不会使用数据(TMax,TMin)构建数组。如果我直接选择它正确生成的城市。我告诉你我的代码。首先是我在html文件中的代码。

<form action="highcharts.php" method="POST">
<b>City:</b> <select name="City">
<option>London</option>^M
<option>Paris</option>^M
</select><br><br>
<button type="submit">Show Graphic</button><br>
</form>

以下是highcharts.php的代码

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var chart;
$(document).ready(function() {
$.getJSON("mysql-highcharts.php", function(json) {

chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'spline',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'TMax-TMin',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Temperature (ºC)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: json
});
});

});

});
</script>
</head>
<body>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
</body>
</html>

最后,代码咨询基础日期mysql-highcharts.php

<?php
$con = mysqli_connect("xxxxx","xxxx","xxxx","xxx");

if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$sth = mysqli_query($con,"SELECT City,TMax FROM Meteo2 where City= '" . $_POST["City"] ."' order by Data");
$rows = array();
$rows['name'] = 'TMAX';
while($r = mysqli_fetch_array($sth)) {
$rows['data'][] = $r['TMax'];
}

$sth = mysqli_query($con,"SELECT City,TMin FROM Meteo2 where City= '" . $_POST["City"] ."' order by Data");
$rows1 = array();
$rows1['name'] = 'TMIN';
while($rr = mysqli_fetch_assoc($sth)) {
$rows1['data'][] = $rr['TMin'];
}

$result = array();
array_push($result,$rows);
array_push($result,$rows1);

print json_encode($result, JSON_NUMERIC_CHECK);

mysqli_close($con);
?>

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

<select name="City">
<option value="London">London</option>
<option value="Paris">Paris</option>
</select>
<select>中缺少

值attribut,可能导致空白$_POST mysql-highcharts.php

修改
好的,由于action =“highcharts.php”, highcharts.php

可以访问$ _POST ['City']

添加

   <?php
   session_start(); 
    $_SESSION['city'] = $_POST['City'];
 ?>

highcharts.php

<?php
session_start();
$con = mysqli_connect("xxxxx","xxxx","xxxx","xxx");

if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$sth = mysqli_query($con,"SELECT City,TMax FROM Meteo2 where City= '" . $_SESSION['city'] ."' order by Data");
$rows = array();
$rows['name'] = 'TMAX';
while($r = mysqli_fetch_array($sth)) {
$rows['data'][] = $r['TMax'];
}

$sth = mysqli_query($con,"SELECT City,TMin FROM Meteo2 where City= '" .$_SESSION['city'] ."' order by Data");
$rows1 = array();
$rows1['name'] = 'TMIN';
while($rr = mysqli_fetch_assoc($sth)) {
$rows1['data'][] = $rr['TMin'];
}

$result = array();
array_push($result,$rows);
array_push($result,$rows1);

print json_encode($result, JSON_NUMERIC_CHECK);

mysqli_close($con);
?>