douyan1896 2014-09-15 09:32
浏览 40
已采纳

使用highcharts传递$ _POST [“City”] mysql

I try to generate highcharts graphics. If I select a city and pass it as $_POST["City"] it doesn't build the arrays with the data (TMax, TMin) when it consults to my basedate mysql. If I choose directly the city it generates correctly. I show you my code. First the code I have inside html file.

<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>

Here comes the code of 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>

And at the end, the code consulting to base-date 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);
?>

Any help please?

  • 写回答

1条回答 默认 最新

  • dqwp81696 2014-09-15 09:47
    关注
    <select name="City">
    <option value="London">London</option>
    <option value="Paris">Paris</option>
    </select>
    

    value attribut is missing in <select> it may cause blank $_POST to mysql-highcharts.php

    Edit
    Ok, Due to action="highcharts.php", $_POST['City'] will be accessible to highcharts.php

    Add

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

    To highcharts.php And

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度