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 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证