2017-08-30 104 views
1

我想绘制从数据谷歌图表(AJAX调用)如何通过ajax调用获取价值回报?

var jsonPieChartData = $.ajax({ 
       url: "ajax.php", 
       data: {selValue1 : 1,selValue2 : 1} , 
       dataType: "json", 
       async: false 
       }).responseText; 
     //create our data table out of json data loaded from server 
     console.log(jsonPieChartData); 

这是ajax.php文件返回

function interncompany(){ 

global $DB; 

//query by experience total post 
$sql = 'SELECT lc.id, count(ljj.job_id) as count, lc.companyname FROM {local_jobs_job} ljj INNER JOIN {local_companydetail} lc ON ljj.job_company_userid = lc.userid where ljj.job_type = 1 group by lc.companyname'; 


//get the query into record 
$data = $DB->get_records_sql($sql); 

//put the query into array 
$rows = array(); 

$rows = array_map(function($item) { 
return (object) ['c' => [ 
    (object) ['v' => $item->companyname, 'f' => null], 
    (object) ['v' => intval($item->count), 'f' => null] 
]]; 
}, array_values($data)); 


$cols = [ 
(object) ['id' => '', 'label' => 'LABEL', 'pattern' => '', 'type' => 'string'], 
(object) ['id' => '', 'label' => 'TOTAL', 'pattern' => '', 'type' => 'number'], 
]; 

$returndata = new stdClass; 
$returndata->cols = $cols; 
$returndata->rows = $rows; 

echo json_encode($returndata); 

} 

数据被动态地选择使用选择框的值

if ($select1 == '1') { 

    if ($select2 == '1') { 

       jobcompany(); 

       } 

    if ($select2 == '2') { 

       joblocation(); 
       } 

     if ($select2 == '3') { 
       jobcategory(); 
     } 


     if ($select2 == '4') { 
       jobsalary(); 
     } 

     if ($select2 == '5') { 
       jobexperience(); 
     } 


     if ($select2 == '6') { 
       joblevel(); 
     } 

} 

elseif ($select1 == '2') { 

    if ($select2 == '1') { 

       interncompany(); 

       } 

    if ($select2 == '2') { 
       internlocation(); 
       } 

     if ($select2 == '3') { 
       interncategory(); 
     } 


     if ($select2 == '4') { 
       internsalary(); 
     } 

     if ($select2 == '5') { 
       internexperience(); 
     } 


     if ($select2 == '6') { 
       internlevel(); 
     } 
    } 

我的问题:我如何创建一个函数来动态获取数据并在数据中插入结果:{}以便数据返回到php文件中,使得t帽子可以通过ajax调用来绘制图表。

现在它什么都没有返回。我需要在数据中进行硬编码:在Ajax中绘制图表。

数据通过这个动态的选择:

// get the select value 
      $(document).ready(function() { 
      // for post-filter 
       $('#post-filter').on('change',function(){ 
       var select1 = $(this).val(); // Post filter value 
       var select2 = $("#field-filter").val(); // Field Filter value 
       $.ajax({ 
         type: 'POST', 
         url: 'ajax.php', 
         data: {selValue1 : select1,selValue2 :select2 }, 
         success: function(result){ 
          console.log(result); 
         } 
        }); 
      }); 



      // field filter value. 
       $('#field-filter').on('change',function(){ 

        var select2 = $(this).val(); // Field filter value 
        var select1 = $("#post-filter").val(); // post Filter value 
        $.ajax({ 
          type: 'POST', 
          url: 'ajax.php', 
          data: {selValue1 : select1,selValue2 :select2 }, 
          success: function(result){ 
           console.log(result); 
          } 
         }); 
        });   
      }); 

这是结果点击选择框时......只有结果没有回到阿贾克斯。要填充内部数据:{}将结果放入ajax调用的drawitem函数中?

enter image description here

错误时插入报头

enter image description here

数据出现,但得到误差和图表是不存在的结果。

+0

'被选择的数据为场滤波器的例子动态使用选择框 - 这是什么代码?它与其他代码的关系在哪里? –

+0

'console.log(jsonPieChartData);'这是什么输出?同样,'async:false' - 主线程上的同步XMLHttpRequest已被弃用 - 这最终可能会失败,您需要学习如何异步执行操作 –

+0

如果我没有设置数据,控制台日志输出为空:{}硬编码里面。 – joun

回答

0

你可以试试下面的方法吗?我已示出,其可以复制用于后置滤波器,如果它工作

var dataVars = {}; 
dataVars['selValue1'] = $("#post-filter").val(); // Post filter value 
dataVars['selValue2'] = $("#field-filter").val(); // Field Filter value 


var jsonPieChartData = $.ajax({ 
       url: "ajax.php", 
       data: JSON.stringify(dataVars) , 
       dataType: "json", 
       async: false 
       }).responseText; 
} 

还要确保内容类型首部设置为JSON在PHP

header('Content-Type: application/json'); 
+0

where我应该插入标题吗?当我把它错误 – joun

+0

错误我显示在问题 – joun

+0

你应该在php文件中的任何地方的PHP文件 –