Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Trying to pass a php variable into js

I have the $attr array that contains the id and footer caption of each graph (highcharts) in a post. When i print_r the array it groups the id and footer_caption correctly and in the right order as they are in the post. For eg in the current post im working at i have 3 graphs and this is what's inside $attr:

    Array
(
    [chart] => 23
    [footer_caption] => footer test
)

    Array
(
    [chart] => 22
    [footer_caption] => another test
)

    Array
(
    [chart] => 24
    [footer_caption] => And another test
)

I'm passing $attr['footer_caption] to javascript like this:

<script>
  var captionLabel = "<?php echo $attr['footer_caption']; ?>";
  console.log(captionLabel);
</script>

And its working fine. Then the problem happens when i use captionLabel on te js file. I tried using a for loop but with no luck. If i console.log captionLabel in the js file, it shows the footer caption of the last graph 3 times. This is the highcharts.js where im using captionLabel:

Highcharts.setOptions({
  credits: {
          enabled: false
  },
  chart: {
          type: 'column',
          events: {
              load: function () {
                var label = this.renderer.label(captionLabel)
                  .css({
                      width: '400px',
                      fontSize: '9px'
                  })
                  .attr({
                      'r': 2,
                      'padding': 5
                  })
                  .add();

                  label.align(Highcharts.extend(label.getBBox(), {
                      align: 'center',
                      x: 0, // offset
                      verticalAlign: 'bottom',
                      y: 20 // offset
                  }), null, 'spacingBox');

              }
          },
          marginBottom: 120
      },
      legend: {
        align: 'center',
        verticalAlign: 'bottom',
        y: -30
    },

My question is how can i pass the right footer_caption string to each graph in this js? Because right now every footer label is getting the last value. All the 3 graphs get the footer caption "And another test" in this post.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Use json_encode()

<?php
$labels = [
[
    ['chart'] => 23,
    ['footer_caption'] => 'footer test 1'
],[
    ['chart'] => 24,
    ['footer_caption'] => 'footer test 2'
],[
    ['chart'] => 25,
    ['footer_caption'] => 'footer test 3 '
]
];
?>
<script>
  var captionLabels = "<?php echo json_encode($labels); ?>";
  console.log(captionLabels);
  var label;
  //access each one like this
  captionLabels.forEach(function(label) {
     label = this.renderer.label(label.footer_caption);
     //graph code.
  });
</script>
over 4 years ago · Santiago Trujillo Report

0

captionLabel has to be an array, otherwise it will only hold the last value assigned to it. You should also use an array for all the $attr values and then use json_encode when outputting itto the JavaScript.

<?php
$attr = [
    ['chart' => '23', 'footer_caption' => 'footer test'],
    ['chart' => '22', 'footer_caption' => 'another test'],
    ['chart' => '24', 'footer_caption' => 'And another test']
];
?>
<script>
    var captionLabel = <?= json_encode($attr) ?>;
</script>
over 4 years ago · Santiago Trujillo Report

0

I think you should generate the array of footer_caption text and send it to javascript as a JSON string.

PHP code:

$array = new array();
foreach ($attr as $value) {
    array_push($array, $value['footer_caption']);
}
$captionLabel = json_encode($array);

JS code:

<script>
     var captionLabel = JSON.parse("<?php echo $captionLabel; ?>");
     console.log(captionLabel);
</script>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!