1
Good evening guys, I needed to run PHP and javascript "at the same time", I need to update a graphic done in javascript without stopping running PHP, I simplified the problem in the code below, if I can run the code below, I can achieve the goal. If anyone knows how to accomplish this goal otherwise please help me !
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">google.charts.load('current', {'packages':['corechart']}); </script>
</head>
<?php
@ini_set("output_buffering", "Off");
@ini_set('implicit_flush', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('max_execution_time',0);
header( 'Content-type: text/html; charset=utf-8' );
echo "Contador: ";
$i = 0;
while (true) { // Enquanto esse looping não terminar nunca vou exibir o gráfico
// depois que i = 5, o gráfico é mostrado, e esse é o problema, rodo uma coisa depois outra, e não as 2 ao mesmo tempo
for (; $i < 5; $i++) {
echo $i." -- ";
if(sleep(1) != 0){
echo "sleep failed script terminating";
break;
}
flush();
ob_flush();
}
printf('
<script type="text/javascript">
i = '.$i.'
alert(i) // Engraçado que isso funciona ! quando i = 5 o alert é mostrado, mas o gráfico não é exibido !
var dashboardState = "";
options = ""
function updateDraw() {
chart = new google.visualization.PieChart(document.getElementById("piechart"));
data = google.visualization.arrayToDataTable(dashboardState);
chart.draw(data, options);
}
VariavelPHP = "LoL =D"
options = {title: "Teste"};
dashboardState = eval("[[\'asd\', \'qwe\'],[\'VariavelPHP\', i],[\'Task2\', 25],[\'Task3\', 10],]");
google.charts.setOnLoadCallback(updateDraw);
</script>
');
$i = 0;
}
?>
<body>
<div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>
If While (true) is removed and "looping is" reaches $i = 5, the graph is shown, "BUT", if While (true) is maintained, then the graph will never be shown !
Thank you for your patience !
You can use ajax. You cannot run at the same time, because js is client-side and php server-side, but you can make js call a php via ajax.
– João Sobral