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?

Share Improve this question asked Apr 8, 2014 at 2:20 MagentoManMagentoMan 5734 gold badges8 silver badges19 bronze badges 1
  • 1 Have sa look at the generated source code in your browser. – Felix Kling Commented Apr 8, 2014 at 2:24
Add a ment  | 

1 Answer 1

Reset to default 6

Try to remove quotes.

var test = <?php echo $monthlycalories; ?>; 

本文标签: Passing php variable to external javascript fileStack Overflow