简体   繁体   English

通过Ajax在PHP和Google Charts之间传递多个数据数组

[英]Passing multiple data arrays between PHP and Google Charts via ajax

I am successfully preparing Google charte using the following ajax call. 我正在使用以下ajax调用成功准备Google图表。 My script, myscript.php reads the database, prepares the chart columns and rows ($array) and then ends with "echo json_encode($array)". 我的脚本myscript.php读取数据库,准备图表的列和行($ array),然后以“ echo json_encode($ array)”结尾。

However, I need myscript.php to also assemble some header and footer HTML that will appear above and below the actual chart. 但是,我还需要myscript.php来组装一些出现在实际图表上方和下方的页眉和页脚HTML。 I've wasted most of the day trying to find how to pass both the data array ($array) plus two other variables ($header and $footer ) back to the drawChart() JavaScript, which would then present the final code. 我整天大部分时间都在浪费时间,试图找到如何将数据数组($ array)以及其他两个变量($ header和$ footer)传递回drawChart()JavaScript,然后再显示最终代码。

Can someone please help or point me in the right direction before I go nuts. 在我发疯之前,有人可以帮忙还是指导我正确的方向。 Thanks! 谢谢!

    function drawChart() {
    var jsonData = $.ajax({
        url: "myscript.php",
        dataType:"json",
        async: false
        }).responseText;  
    var dataTable = new google.visualization.DataTable(jsonData);
    var options = {
        'is3D':                     false,
        'width':                    310
    };
    var chart = new google.visualization.PieChart(document.getElementById('chart'));
    chart.draw(dataTable, options);
    }

Can you not generate the HTML on the client side? 您不能在客户端生成HTML吗?
If you really need to you can send the HTML as objects in the JSON.. 如果确实需要,可以将HTML作为JSON中的对象发送。

{ "header": "<h2>Header</h2>", "footer": "<p>Footer</p>", "data": [] }

Make sure you understand how JSON works as an easy way to transmit information between clients and servers, among other things. 确保了解JSON是如何在客户端和服务器之间传输信息的简便方法。 Understand $header and $footer are not readily available on the client machine (browser); 了解$ header和$ footer在客户端计算机(浏览器)上不可用; your script must convert them to a means that transferrable, which is why JSON format works so well (browsers implement Javascript) 您的脚本必须将它们转换为可转让的方式,这就是JSON格式如此出色的原因(浏览器实现Javascript)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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