gpt4 book ai didi

javascript - PHP 和 Ajax : using post ajax return error

转载 作者:行者123 更新时间:2023-11-29 15:42:12 24 4
gpt4 key购买 nike

我有从数据库返回的数据,我使用 Chartjs 将它们显示在饼图上。

我希望用户选择特定的日期范围,当她单击过滤器时,它应该根据用户选择的日期在饼图上显示数据,live demo

这是我的解决方案。

HTML

    from <input type="text" id = "firstdatepicker" name = "firstdatepicker">
to <input type="text" id = "lastdatepicker" name = "lastdatepicker">
<input type="button" name="filter" id="filter" value="Filter" class="btn btn-info" />

JS

$(document).ready(function(){

$(function() {
$( "#firstdatepicker" ).datepicker();
$( "#lastdatepicker").datepicker();
});

$('#filter').click(function(){
var from_date =$('#firstdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
var to_date =$('#lastdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
if(from_date != '' && to_date != ''){

$.ajax({
url:"https://meed.audiencevideo.com/admin/chats/stats.php",
type:"POST",
data:{from_date:from_date, to_date:to_date},
success:function(data){
console.log(data);
var session= data[0].sessions;
var yes = data[0].num_yes;
var no =data[0].num_no;
var ctx = document.getElementById("myPieChart");
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["sessions","yes", "no"],
datasets: [{
label: 'Genders',
data: [session,yes, no],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 1)'
],
borderWidth: 1
}]
},
});
},
error: function () {
console.log('Something went wrong');
}
});
}
else
{
alert("Please Select Date");
}
});
})

这是从数据库中获取数据的php

if (isset($_POST['from_date']) && isset($_POST['to_date'])) {
$firstDate= $_POST['from_date'];
$lastDate= $_POST['to_date'];
$firstDate_new = date('Y-m-d', strtotime($firstDate));
$lastDate_new = date('Y-m-d', strtotime($lastDate));

$query = sprintf("SELECT count(*) as num_rows, datetime, count(distinct sid) as sessions, sum( targetbuttonname = 'yes' ) as num_yes, sum( targetbuttonname = 'no' ) as num_no from events WHERE datetime BETWEEN '{$firstDate_new}' AND '{$lastDate_new}'");
var_dump($query);

$result = $mysqli->query($query);
$data = [];
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$data[] = $row;
}
}

echo json_encode($data);
exit;

}else{

//query to get data from the table
$query = sprintf("SELECT count(*) as num_rows, count(distinct sid) as sessions, sum( targetbuttonname = 'yes' ) as num_yes, sum( targetbuttonname = 'no' ) as num_no from events;");
}

$result = $mysqli->query($query);

//loop through the returned data
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
//free memory associated with result
$result->close();

//close connection
$mysqli->close();

//now print the data
print json_encode($data);

当我选择日期范围并单击过滤器时,我会在这样的控制台上获得所需的数据。

"SELECT count(*) as num_rows, datetime, count(distinct sid) as sessions, sum( targetbuttonname = 'yes' ) as num_yes, sum( targetbuttonname = 'no' ) as num_no from events WHERE datetime BETWEEN '2019-08-13' AND '2019-08-14'"


[
{
"0":"12",
"num_rows":"12",
"1":"2019-08-14",
"datetime":"2019-08-14",
"2":"12",
"sessions":"12",
"3":"2",
"num_yes":"2",
"4":"4",
"num_no":"4"
}
]

我从 ajax 中收到错误出了点问题。当我尝试从 POST 更改为 GET 时,会显示数据,但旧数据。

我的代码做错了什么?

最佳答案

用下面的代码改变你的ajax部分

$(document).ready(function(){
$(function() {
$( "#firstdatepicker" ).datepicker();
$( "#lastdatepicker").datepicker();
});
$('#filter').click(function(){
var from_date =$('#firstdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
var to_date =$('#lastdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
if(from_date != '' && to_date != ''){
$.ajax({
url:"https://meed.audiencevideo.com/admin/chats/stats.php",
type:"POST",
dataType:"json",
data: {
from_date: from_date,
to_date: to_date
},
success:function(data){
console.log(data);
var session= data[0].sessions;
var yes = data[0].num_yes;
var no =data[0].num_no;
var ctx = document.getElementById("myPieChart");
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["sessions","yes", "no"],
datasets: [{
label: 'Genders',
data: [session,yes, no],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 1)'
],
borderWidth: 1
}]
},
});
},
error: function () {
console.log('Something went wrong');
}
});
} else {
alert("Please Select Date");
}
});
});

关于javascript - PHP 和 Ajax : using post ajax return error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57494100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com