帮助我如何发布值年份表单下拉列表然后它将使用ajax加载到JSON数据中。修复我的代码请我是一个喜欢学习更多的学生。如果错误,我感谢您为检查我的代码所做的贡献。我不熟悉ajax,但我正在尝试。
<script>
$(document).ready(function () {
getGraph();
function getGraph()
{
$.getJSON("bar_graph_query.php", function (result)
{
var chart = new CanvasJS.Chart("chartBar", {
animationEnabled: true,
theme: "light2",
title:{
text: "Summary Rent Profit"
},
axisY: {
title: "Total (RM)"
},
data: [{
dataPoints: result,
}]
});
chart.render();
});
}
$.("#years").onchange(function(){
getGraph();
});
});
</script>
脚本ajax将数据加载到json(bar_graph_summary.php)
<div class="label">Select Name:</div>
<select class="form-control" name="years" id="years" style="width: 120px" >
<option value="'">Select Years</option>
<?php
include('db_conn.php');
$query = $conn->query("SELECT YEAR(rent_date) as years FROM rent GROUP BY YEAR(rent_date)");
while($row = $query->fetch_assoc())
{
echo '<option value="'.$row['years'].'">'.$row['years'].'</option>';
}
?>
</select>
下拉选择年份(bar_graph_summary.php)
<?php
include('db_conn.php');
$data_points = array();
$years = $_REQUEST['years'];
$query = mysqli_query($conn, "SELECT MONTHNAME(rent_date) as month, SUM(rent_total)as total FROM rent WHERE YEAR(rent_date)='$years' GROUP BY MONTH(rent_date)");
//$query = mysqli_query($conn, "SELECT MONTHNAME(rent_date) as month, SUM(rent_total)as total FROM rent GROUP BY MONTH(rent_date)");
while($row = mysqli_fetch_array($query))
{
$point = array("label" => $row['month'] , "y" => $row['total']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
?>
查询数据json(bar_graph_query.php)