0

I need a help with using PHP variable in JavaScript situation. I want to use PHP variable in a morris chart such as,

var area = new Morris.Area({
                    element: 'revenue-chart',
                    resize: true,
                    data: [
                        {y: '6D', fab: 20004, qc: 0},
                        {y: '5D', fab: 2778, qc: 0},
                        {y: '4D', fab: 4912, qc: 0},
                        {y: '3D', fab: 3767, qc: 0},
                        {y: '2D', fab: 6810, qc: 0},
                        {y: '1D', fab: <?php echo $sumWeightYesterday; ?>, qc: 0},
                        {y: 'NOW', fab: 4820, qc: 0}
                    ],
                    parseTime: false,
                    xkey: 'y',
                    ykeys: ['fab', 'qc'],
                    labels: ['FAB', 'QC'],
                    lineColors: ['#a0d0e0', '#3c8dbc'],
                    hideHover: 'auto'
                });

I want to use $sumWeightYesterday but it displays 0 on the data.

Please help me, Thanks

8
  • 3
    Looks correct. If this is in an external JS file, it has to be .php too and served with appropriate mime headers Commented Sep 9, 2014 at 4:29
  • 2
    What does the generated code look like? I.e. the code the browser evaluates? Commented Sep 9, 2014 at 4:29
  • 3
    Assuming this file is executed by PHP and assuming that $sumWeightYesterday actually has a value (not 0), you should always use json_encode() when injecting into JavaScript. For example fab: <?= json_encode($sumWeightYesterday) ?> Commented Sep 9, 2014 at 4:32
  • 1
    @Phil, didn't know about "always should use json_encode()" — any reason for that? Commented Sep 9, 2014 at 4:33
  • 2
    @l'L'l it just sanitises your variable for use in JavaScript. For example, strings will be quoted correctly without having to worry about escaping single / double quotes, etc. Commented Sep 9, 2014 at 4:37

1 Answer 1

1

You can try it.

var sumWeightYesterday = <?php echo $sumWeightYesterday; ?>

var area = new Morris.Area({
                    element: 'revenue-chart',
                    resize: true,
                    data: [
                        {y: '6D', fab: 20004, qc: 0},
                        {y: '5D', fab: 2778, qc: 0},
                        {y: '4D', fab: 4912, qc: 0},
                        {y: '3D', fab: 3767, qc: 0},
                        {y: '2D', fab: 6810, qc: 0},
                        {y: '1D', fab: sumWeightYesterday, qc: 0},
                        {y: 'NOW', fab: 4820, qc: 0}
                    ],
                    parseTime: false,
                    xkey: 'y',
                    ykeys: ['fab', 'qc'],
                    labels: ['FAB', 'QC'],
                    lineColors: ['#a0d0e0', '#3c8dbc'],
                    hideHover: 'auto'
                });
Sign up to request clarification or add additional context in comments.

Comments

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.