有没有办法在javascript代码中插入php变量?

时间:2014-04-03 20:56:33

标签: javascript php

我正在使用jQuery插件创建一个折线图,脚本如下

<script>

var lineChartData = {
    labels : ["1","2","3","4","5","6","7","8","9","10","11","12"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            pointColor : "rgba(220,220,220,1)",
            pointStrokeolor : "#fff",
            data : [5,6,19,34,59,93,128,158,206,266,329,398,418]
        },

        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            pointColor : "rgba(151,187,205,1)",
            pointStrokeColor : "#fff",
            data : [0,0,0,3,15,46,68,109,169,228,319,364,386]
        }
        ]   

    }

var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);

我想将变量插入var的数据部分,例如

data : [$q1,$q2,$q3, etc, etc]

我不知道javascript,所以我不知道从哪里开始。我该怎么做?

我将变量放在一个单独的文件中并将其包含在此文件中。

1 个答案:

答案 0 :(得分:6)

渲染页面时,您可以在PHP中删除:

data : [<?php echo "$q1,$q2,$q3, etc, etc";?>]