使用AJAX检索数据

时间:2017-07-07 12:47:13

标签: javascript json ajax

我有拖曳文件 第一个文件(pie_chart.php)生成多个查询,以便每周或每天或每月从数据库中选择数据 这些数据将通过json发送到另一个文件(test.php) 此文件将检索此json数据以使用谷歌图表api绘制图表 这是 test.php

的内容



function pie_chart() {
		
      var jsonData = $.ajax({
          url: "pie_chart.php",
          dataType: "json",
          async: false
          }).responseText;
          
      // Create our data table out of JSON data loaded from server.
 alert(jsonData);
      var data = new google.visualization.DataTable(jsonData);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.LineChart(document.getElementById('piechart_div'));
      chart.draw(data, {width: 600, height: 400});
	}
	


</script>
&#13;
&#13;
&#13;

现在的问题是 如何在条件之后告诉ajax从pie_chart.php获取数据 即 在pie_chart.php中有

&#13;
&#13;
<?php
include ("inc/db_settings.php");
if($_POST['tar']=='week')
{$sql="SELECT count(username) as count, date(AcctStartTime) AS date1 FROM radacct where date(AcctStartTime) >= curdate() - INTERVAL DAYOFWEEK(curdate())+30 DAY  AND date(AcctStartTime) < curdate() - INTERVAL DAYOFWEEK(curdate())-3 DAY GROUP BY date1";

#$sql = "SELECT
   
   # `cnumber`AS `count`,`date` 
#FROM `chart` WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
#AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY";
#$sql="SELECT COUNT(DISTINCT `username`) AS `count`,date(`AcctStartTime`) AS `date` FROM `radacct` where date(AcctStartTime) = curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY  AND date(AcctStartTime) < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY";

/*
SELECT count(username) as count, date(AcctStartTime) AS date1 
FROM radacct where date(AcctStartTime) >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY  AND date(AcctStartTime) < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
GROUP BY date1
*/
$query = mysql_query($sql);
while($result = mysql_fetch_array($query))
{
  $rows[]=array("c"=>array("0"=>array("v"=>$result['date1'],"f"=>NULL),"1"=>array("v"=>(int)$result['count'],"f" =>NULL)));
  
}

echo $format = '{
"cols":
[
{"label":"date1","pattern":"","type":"string"},
{"label":"count","pattern":"","type":"number"}
],
"rows":'.json_encode($rows).'}';
}

else if($_POST['month'])
{
	$sql="select `employee_id`,`date`,`checkin`,`checkout`
from `attendence` 
where `date` BETWEEN  MONTH('01-10-2012')
AND  MONTH( '31-10-2012')";
	//do some thing
	
	
	
	
}?>
&#13;
&#13;
&#13;

我想在用户选择周期时使用谷歌图表制作图表(每月,每周,每年),例如从选择对象 我使用ajax将用户的选择发送到pie_chart.php并对其进行查询延迟 之后,我将结果转换为json,将其发送到test.php,它将检索此json数据以生成图表 希望我能清楚地解释我的问题

1 个答案:

答案 0 :(得分:0)

在Ajax中使用数据:发送数据参数

var aData= [];
aData[1]="Some value";
    var jsonData = JSON.stringify({ aData:aData});
    $.ajax({
        type: "POST",
         url: "pie_chart.php", 
        data: jsonData,
        contentType: "application/json; charset=utf-8",
        dataType: "json", // dataType is json format
        success: OnSuccess,
        error: OnErrorCall
    });

    function OnSuccess(response) {

    }
    function OnErrorCall(response) { console.log(error); }
    });