I've been trying to use a php variable inside a JS script to no avail, I've read three threads including: how to use PHP variable in Javascript Access PHP variable in JavaScript How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>
But I still cannot manage to make it work for myself, I'm trying to make a chart with but replacing the names inside with php variables that I will get/give: https://developers.google.com/chart/interactive/docs/gallery/orgchart?csw=1
my code:
<?php
$post_grandfather = "Dodo";
echo $post_grandfather;
?>
<html>
<head>
<script type="text/javascript">
var myvariable1 = "<?php echo $post_grandfather; ?>";
var a = <?php echo json_encode($post_grandfather); ?>;
</script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {packages:["orgchart"]});
google.charts.setOnLoadCallback(drawChart);
document.write(5 + 6);
function drawChart() {
var data = new google.visualization.DataTable();
var myvariable = <?php echo json_encode($post_grandfather); ?>;
data.addColumn('string', 'Name');
data.addColumn('string', 'Manager');
data.addColumn('string', 'ToolTip');
// For each orgchart box, provide the name, manager, and tooltip to show.
data.addRows([
[{'v':String(a), 'f':String(a)'Mike<div style="color:red; font-style:italic">President</div>'},
'', 'The President'],
[{'v':'Jim', 'f':'Jim<div style="color:red; font-style:italic">Vice President</div>'},
'Mike', 'VP'],
['Alice', 'Mike', ''],
['Bob', 'Jim', 'Bob Sponge'],
['Carol', 'Bob', ''],
['Hamad','Alice', '']
]);
// Create the chart.
var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
// Draw the chart, setting the allowHtml option to true for the tooltips.
chart.draw(data, {'allowHtml':true});
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
Please help