3

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
  • 1
    Have sa look at the generated source code in your browser. Commented Apr 8, 2014 at 2:24

1 Answer 1

6

Try to remove quotes.

var test = <?php echo $monthlycalories; ?>; 
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks @sectus! I can't believe that is what caused it. I spent hours messing with this. Thanks again!
As mentioned above: explore your page source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.