i'm trying to use highcharts using json data from mysql table. i want to set a drop down list with values that are going to be used as WHERE conditions for the mysql_query to fetch selected data and populate them in highchart. the codes are working properly with static conditions (no drowpdown variables).
main.html
<form method="get" action="" >
Chose :
<select name="liste" id="liste">
<option value="A" <? if($selected == 'A'){ echo 'selected="Choice A"';}?>Choice A</option>
<option value="B" <? if($selected == 'B'){ echo 'selected="Choice B"';}?>Choice B</option>
<option value="C" <? if($selected == 'C'){ echo 'selected="Choice C"';} ?>Choice C</option>
<option value="D" <? if($selected == 'D'){ echo 'selected="Choice D"';} ?>Choice D</option>
</select>
<input type="submit" value="Go" />
</form>
data.php
`<?php
$A=$_GET['liste'];
$con = mysql_connect("localhost","myuser","mypwd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);
$test = mysql_query("SELECT name FROM table1 WHERE table2.age LIKE '{$A}'");
$rowss = array();
while($rr = mysql_fetch_array($test)) {
$ro[0] = $rr[0];
$ro[1] = $rr[1];
array_push($rowss,$ro);
}
print json_encode($rowss, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
on the main.html file i have the highchart script that call the data.php file to get the json data.
<script>
$(document).ready(function() {
//rest of the code
$.getJSON("data.php", function(json) {
options.series[0].data = json;
chart = new Highcharts.Chart(options);
});
</script>
testing the data.php give the correct output according to the selected value. so i'm assuming that it is working fine. my problem is when loading the main.html it draws an empty graph and even when changing the value keep drawing empty data graph.
NB: i know mysql_*
is no longer best practice and planing to switch to mysqli_*
later.
EDIT:
seems like my options.series[0]
options obj is not created and the console shows empty data array. when using <form method="get" action="data.php" >
i got the expect array but cannot get the graph.
I got works as i wanted according to the following example i had to make ajax call to set the variable onchange of the drop menu and the code become like this (remove the form tag and submit button) main.html:
and the data.php:
enjoy it!!
i try this query it give perfect graph.