我正在研究一个将显示条形图的项目。条形图工作正常,但是当我尝试将一个整数php变量传递给javascript变量并添加变量作为列表元素不起作用时。下面是我的代码。我不知道我的源代码有什么问题。我花了几个小时在网上寻找解决方案,但我没有得到任何答案。如何将php变量作为javascript中的列表元素
期待看到您的回复。
<html>
<head><title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
#container {
height: 400px;
min-width: 310px;
max-width: 800px;
margin: 0 auto;
}
</style>
<?php
//my php variable
$ab=6;
?>
<script type="text/javascript">
//assign the php variable to javascript variable
var za = <?php echo $ab; ?>;
$(function() {
$('#container').highcharts({
chart: {
type: 'column',
options3d: {
enabled: true,
alpha: 10,
beta: 25,
depth: 70
}
},
title: {
text: '3D chart with null values'
},
subtitle: {
text: 'Notice the difference between a 0 value and a null point'
},
plotOptions: {
column: {
depth: 25
}
},
xAxis: {
categories: Highcharts.getOptions().lang.shortMonths
},
yAxis: {
title: {
text: null
}
},
series: [{
name: 'Sales',
// I'm putting the variable as an element below, but it's not working
data: [za, 3, null, 4, 0, 5, 1, 4, 6, 3]
}]
});
});
</script>
</head>
<body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-3d.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="height: 400px"></div>
</body>
看起来它应该工作得很好。你有没有检查PHP的*输出*是什么? – Quentin
假设文件保存为php并执行为php它应该工作。如果你只是将它保存为.html然后不是 – mplungjan
如果我在浏览器中访问页面时只是看到空白页面(即没有任何内容的页面),但是如果用数字条形图替换数据列表中的'za'变量显示在页面 – dyahmed