admin管理员组文章数量:1334133
I have seen many solutions to this question on this site. However, nothing I try seems to be working.
In my php file I have this:
<?php
$monthlycalories = "[1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]";
?>
<script type="text/javascript">
var test = "<?php echo $monthlycalories; ?>";
</script>
In my javascript file I have this:
$(function () {
var example = test;
alert(example);
$('#monthly').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Monthly Calorie Intake'
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
},
yAxis: {
min: 0,
title: {
text: 'Calories'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0"></td>' +
'<td style="padding:0"><b>{point.y:.1f} Calories</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'Months',
data: example
}]
});
});
The javascript is a chart. I have been able to pass the info into the javascript file. I have used the alert()
function and can see that it indeed brought the variable over. However, when I use the example
variable in the javascript it does not work. Furthermore, if I copy over the alert results and replace data: example
with data: [1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]
then it works.
I can't understand why the variable example
will not work but the chart works fine if you enter the info manually?
I have seen many solutions to this question on this site. However, nothing I try seems to be working.
In my php file I have this:
<?php
$monthlycalories = "[1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]";
?>
<script type="text/javascript">
var test = "<?php echo $monthlycalories; ?>";
</script>
In my javascript file I have this:
$(function () {
var example = test;
alert(example);
$('#monthly').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Monthly Calorie Intake'
},
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
},
yAxis: {
min: 0,
title: {
text: 'Calories'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0"></td>' +
'<td style="padding:0"><b>{point.y:.1f} Calories</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'Months',
data: example
}]
});
});
The javascript is a chart. I have been able to pass the info into the javascript file. I have used the alert()
function and can see that it indeed brought the variable over. However, when I use the example
variable in the javascript it does not work. Furthermore, if I copy over the alert results and replace data: example
with data: [1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]
then it works.
I can't understand why the variable example
will not work but the chart works fine if you enter the info manually?
- 1 Have sa look at the generated source code in your browser. – Felix Kling Commented Apr 8, 2014 at 2:24
1 Answer
Reset to default 6Try to remove quotes.
var test = <?php echo $monthlycalories; ?>;
本文标签: Passing php variable to external javascript fileStack Overflow
版权声明:本文标题:Passing php variable to external javascript file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742358341a2459863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论