javascript - 如何使用表单中的输入变量来更新另一个 php 的图表?

标签 javascript php jquery html mysql

我正在创建一个报告,当我修改变量时,它可以动态显示图表,我所做的将按照以下步骤进行: 1.在index.html表单中输入变量date并将开始日期和结束日期传递给get_data.php 2. get_data.php 将根据 post 变量日期运行 sql 并获取数组。 3. 基于 get_data.php 中的数组将返回到 index.html 并显示为图表。 这一切只会发生在index.html中,不会去get_data.php

我面临的错误是我可以正确运行get_data.php,但结果无法传回index.html并正确显示。我的代码如下:

index.html

<script type="text/javascript" src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://canvasjs.com/assets/script/canvasjs.min.js"></script> 
<script type="text/javascript">
window.onload = function () {
	var chart = new CanvasJS.Chart("chartContainer", { 
		theme: "theme2",
		title: {
			text: "User"
		},
		data: [
		{
			type: "column",
			dataPoints: [
				<?php require('last_month_report.php'); echo str_replace("}{", "},{", $dataset);?>
					
			]
		}
		]
	});
	chart.render();

	$("#apply").click(function() {
	var data = $("#report :input").serializeArray();
	$.post( $("#report").attr("action"), data, function(updatedata) {
            dataPoints.push(updatedata);
        chart.render();
        updateChart();
    });
	});

	$("#report").submit( function() {
		return false;
	});	
};
</script>
<!DOCTYPE html>
<html> 
<body>
    <form action="get_data.php" method="post" id="report">
        <input type="date" name="start_date" id="start_date">
        <input type="date" name="end_date" id="end_date">
        <button id="apply" type="submit">Apply</button>
    </form>
    <div id="chartContainer"></div>
</body>
</html>

获取数据.php

<?php
$start_date = $_POST[start_date];
				$end_date = $_POST[end_date];
				$link=mysql_connect('localhost','root','123');
				mysql_set_charset('utf8', $link);
				$selectdb=mysql_select_db('test',$link);
				$sql="SELECT date, 	user FROM `report` WHERE date >= '$start_date' and date <= '$end_date'";
				$result = mysql_query($sql, $link);

				$init=0;
				$num=mysql_num_rows($result);
				while ($init < $num) {
					$row=mysql_fetch_array($result);
					$dataset = $dataset . '{label: "' . $row[date] . '",y: ' . $row[date] . '}';
					$init++;
				}
?>

最佳答案

这些行不通

     $start_date = $_POST[start_date];
     $end_date = $_POST[end_date];

缺少"" 应该是

     $start_date = $_POST["start_date"];
     $end_date = $_POST["end_date"];

关于javascript - 如何使用表单中的输入变量来更新另一个 php 的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47226252/

相关文章:

php - 浏览器语言来自 PHP 还是 JavaScript?

javascript - data-XXX 没有返回它应该返回的内容。不断返回为未定义

javascript - php脚本邮件程序模式 Bootstrap

javascript - 使用 Jquery 选择两个下拉值

javascript - 使用 getElementByid 选择所有 ID 或类

Javascript 在调用 super 之前初始化类属性

javascript - 粘贴到 Canvas 中

php - G-WAN,CGI 脚本的输出 header

php - MySQL - mysql_fetch_assoc() 不给出一列

jquery 前一个元素有几个类