Pass php variable in javascript function

Asked

Viewed 49 times

0

Could you help me with a question? I need to pass PHP variable values to a Javascript function (Laravel).

//+5%
$tv90 = number_format($tv90 + ($tv90/100*5), 2, '.', '');
$tv125 = number_format($tv125 + ($tv125/100*5), 2, '.', '');
$tc50 = number_format($tc50 + ($tc50/100*5), 2, '.', '');
$ts100 = number_format($ts100 + ($ts100/100*5), 2, '.', '');
$ts150 = number_format($ts150 + ($ts150/100*5), 2, '.', '');
$tcomp = number_format($tcomp + ($tcomp/100*5), 2, '.', '');
function drawChart() { 
    var data = google.visualization.arrayToDataTable([
     ['Mês', 'DC', 'FUNDIÇÃO', 'BRITAGEM', 'BORRACHA', 'NELES', 'REFORMA'],
    ['JAN', 300, 400, 200, 1000, 400, 200]]);}

    $(window).resize(function () { drawChart();
});

I want to replace the values with variables, load dynamically.

1 answer

0

It is important to know that PHP runs on the server, and returns HTML. Javascript can be written within HTML tags. So to pass a PHP value to Javascript just do:

<?php
$valorNoPHP = 'meu valor veio do PHP';
?>
<script>
var variavelJavaScriptComValorDoPHP = '<?= $valorNoPHP ?>';
</script>

Browser other questions tagged

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