简体   繁体   中英

How to update the chart from another php with input the variable from the form?

I am creating a report which can display a chart dynamically when i modify the variable, what i do will follow the below step: 1. input the variable date in the index.html form and pass the start date and end date to the get_data.php 2. get_data.php will base on the post variable date and run the sql and get the array. 3. base on the array from get_data.php will back to the index.html and display as a chart. All this will only happen in the index.html, won't go to get_data.php

The error I am facing is that I can run the get_data.php correctly but the result can't pass back to the index.html and display correctly. My code as below:

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> 

get_data.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++; } ?> 

these wont work

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

missing " " should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM