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>
Possible duplicate of php value in javascript?
– LipESprY
I saw this question, however, the method there did not work
– Carlos
If the file containing the code in
javascript
for.html
or.js
you will not be able to use the variable. To use the variables and codes in php, the file has to be.php
.– Andrei Coelho
Possible duplicate of Match php variable to javascript variable | PHP variable in Javascript | Pass php variable to javascript | Calling PHP variable inside a Javascript function
– Woss
This subject is quite recurrent here on the site: https://answall.com/search?tab=relevance&q=%5bphp%5d%20%5bjs%5d%20vari%C3%A1vel
– Woss