php values exporting to javascript

Asked

Viewed 115 times

1

I have a code with php and javascript, I would like to know how to use the variable value of my code in php within the java script. Follows the code:

var teste = "<?php echo $teste; ?>";
var chart = new Highcharts.Chart({
  chart: {
    renderTo: 'container',
    type: 'column',
    options3d: {
      enabled: true,
      alpha: 6,
      beta: 16,
      depth: 100,
      viewDistance: 200
    }
  },
  xAxis: {
    type: 'category'
  },
  title: {
    text: 'Clientes'
  },
  subtitle: {
    text: 'Gráfico demonstrativo'
  },
  plotOptions: {
    column: {
      depth: 25
    }
  },
  series: [{
    "colorByPoint": true,
    data: [{
      "name": "Coluna 1",
      "y": teste.9,
      "drilldown": "Coluna "
    }, {
      "name": "Coluna 2",
      "y": 106.4,
      "drilldown": "Coluna 2"
    }, {
      "name": "Coluna 3",
      "y": 1000.5,
      "drilldown": "Coluna 3"
    }, {
      "name": "Coluna 4",
      "y": 144.0,
      "drilldown": "Coluna 4"
    }]
  }]
});

function showValues() {
  $('#alpha-value').html(chart.options.chart.options3d.alpha);
  $('#beta-value').html(chart.options.chart.options3d.beta);
  $('#depth-value').html(chart.options.chart.options3d.depth);
}


$('#sliders input').on('input change', function() {
  chart.options.chart.options3d[this.id] = parseFloat(this.value);
  showValues();
  chart.redraw(false);
});

showValues();
#container,
#sliders {
  min-width: 310px;
  max-width: 800px;
  margin: 0 auto;
}

#container {
  height: 400px;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-3d.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<?php $teste="20"; ?>
<div id="container"></div>
<div id="sliders">
</div>

2 answers

1


Good evening, make sure you are assigning the PHP variable before your echo in javascript, I think you are not doing this.

Example:

<?php 
    $teste = 20;
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Exemplo</title>
    </head>
    <script>
        var teste = <?= $teste ?>;
        console.log(teste);
    </script>
<body>
</body>
</html>

Be sure to put ; after declaring and assigning the variable in javascript. Note that some servers are not enabled to run the output <?= var ?> you can use <?php echo var; ?> despite being less elegant.

Remember PHP runs on server and javascript on client.

  • Excellent! gave mto right. Thanks.

0

you should echo the html and with jquery take this value example :

<div id="container" id="<?= $teste="20"; ?>"></div>

Jquery

$(document).ready(function() { 
        var teste = $("#conteiner").attr('id');
}); 
  • Thanks for the reply. Good to know that there are other ways to achieve the same result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.