如何将php变量导入javascript?

时间:2015-12-09 10:46:06

标签: javascript php

我想将php变量中的数据导入饼图,因为我试图用$ Utha和饼图中的$ 1值来实现。如何使用从数据库连接的示例?如何在同一页面中显示多个图表。例如,每个图表具有不同的数据。这些图表能否居中。

<?php
$Utha = 'Utha';    
$Ilinois = 'Ilinoins';
$WD = 'Washington DC';
$Florida = 'Florida';
$California = 'California';

$one = 1;
$two = 2;
$three = 3;
$four = 4;
$five = 5;
?>
<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['$Utha',     $one],
          ['$Ilinois',  $two],
          ['$Florida',  $three],
          ['$California', $four],
          ['$WD',    $five]
        ]);

        var options = {
          title: 'My Daily Activities',
          is3D: true,
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart_3d" style="width: 900px; height: 500px;"></div>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

您可以基于PHP变量在javascript中创建变量,例如:

var one = '<?php echo $one; ?>';

希望这有帮助。

答案 1 :(得分:0)

var demo = "<?php echo $phpVar; ?>";