0
 <!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</title>
        <script type="text/javascript" src="js/jquery.min.js"></script>
        <script type="text/javascript">
$(function () {
    var newanswer = <?php echo"$newanswer"; ?>
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'column'
            },
            title: {
                text: 'Monthly Progress Reports'
            },
            subtitle: {
                text: 'Source: Sales Department'
            },
            xAxis: {
                categories: [
                    'Jan',
                    'Feb',
                    'Mar',
                    'Apr',
                    'May',
                    'Jun',
                    'Jul',
                    'Aug',
                    'Sep',
                    'Oct',
                    'Nov',
                    'Dec'
                ]
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Range'
                }
            },
            legend: {
                layout: 'vertical',
                backgroundColor: '#FFFFFF',
                align: 'left',
                verticalAlign: 'top',
                x: 100,
                y: 70,
                floating: true,
                shadow: true
            },
            tooltip: {
                formatter: function() {
                    return ''+
                        this.x +': '+ this.y +' Star';
                }
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
                series: [{
                name: '5 Star', //Blue
                data: [newanswer, 20, 30, 40, 50, 60, 70, 80, 90, 95, 10, 20]

            }, {
                name: '4 Star', //Red
                 data: [4, 20, 30, 40, 50, 60, 70, 80, 90, 95, 10, 20]

            }, {
                name: '3 Star', // Green
                data: [3, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]

            }, {
                name: '2 Star', //violet
                data: [2, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]

            }, {
                name: '1 Star', //lightblue
                data: [1, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]

            }]
        });
    });

});
        </script>
    </head>
<body>
<script src="js/highcharts.js"></script>
<script src="js/exporting.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

<?php
$newanswer = 50;
?>

</body>
</html>

is this possible to pass a php value to a javascript? i need to do it in order to put the value of the query of mysql into the graph and the graph is coded by java script. thx

the problem is this line

<?php $newanswer = 50; ?>

cant pass the value on this line:

 var newanswer = <?php echo"$newanswer"; ?>

so that the 50 value dont show in this line:

 data: [newanswer, 20, 30, 40, 50, 60, 70, 80, 90, 95, 10, 20]
4

5 回答 5

3

PHP没有JS的概念,所以你可以简单地......

data: [<?php echo $newanswer ?>, 20, 30, 40, 50, 60, 70, 80, 90, 95, 10, 20]

但是,请注意,直接输出到 JS 代码块意味着您必须格外小心,不要在 JS 代码中引入语法错误。一个错误,整个代码块被 JS 解析器杀死。因此,作为一般规则,您应该始终在 PHP 中使用 json_encode() 来确保生成语法上有效的 JS。

即使它只是你输出的一个整数,这也适用——最好现在就养成 json 编码的习惯。它将为您节省很多痛苦:

data: [<?php echo json_encode($newanswer) ?>, 20, 30, 40, 50, 60, 70, 80, 90, 95, 10, 20]

因此,即使 $newanswer 为 null 或未定义,您最终也会使用 valis JS。

于 2012-10-16T03:58:41.357 回答
2

这部分代码必须一直向上移动:

<?php
$newanswer = 50;
?>

而这部分:

var newanswer = <?php echo"$newanswer"; ?>

最好重写为:

var newanswer = <?php echo json_encode($newanswer); ?>;

编辑

最后一部分可以写得更好,见Marc B's answer

于 2012-10-16T03:58:33.647 回答
0

将隐藏的值放入如下形式

<input type="hidden" name="newanswer" id="newanswer" value="<?=$newanswer; ?>" >

现在使用getElementById你可以获得$newanswer

于 2012-10-16T03:59:15.673 回答
0

您在代码末尾使用 PHP 变量定义它。只需在将其传递给 JavaScript之前定义它。

于 2012-10-16T04:02:25.657 回答
0

ehco 隐藏字段的值

然后转到javascript并添加以下代码

var xi=(document.getElementById("aa")).value;
于 2013-09-05T13:14:27.567 回答