이 사이트에서이 질문에 대한 많은 해결책을 보았습니다. 그러나 내가 시도하는 것은 아무것도 작동하지 않는 것 같습니다. 내 PHP 파일에서외부 자바 스크립트 파일에 PHP 변수 전달하기
나는이 있습니다
<?php
$monthlycalories = "[1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]";
?>
<script type="text/javascript">
var test = "<?php echo $monthlycalories; ?>";
</script>
를 내 자바 스크립트 파일에서 나는이 있습니다
$(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
}]
});
});
자바 스크립트 차트이다. 내가 자바 스크립트 파일에 정보를 전달할 수있게되었습니다. 나는 alert()
함수를 사용했고 실제로 변수를 가져 왔음을 알 수 있습니다. 그러나, 내가 자바 스크립트에서 example
변수를 사용하면 작동하지 않습니다. 또한 경고 결과를 복사하고 data: example
을 data: [1, 1364, 1052, 922, 1, 1, 10, 1, 10, 10, 10, 265]
으로 바꾼다면 작동합니다.
변수 example
이 작동하지 않는 이유를 이해할 수 없지만 수동으로 정보를 입력하면 차트가 제대로 작동합니까?
** 생성 된 ** 소스 코드를 브라우저에서 확인하십시오. –