Run php and javascript at the same time

1

Good evening, I needed to run PHP and javascript "at the same time", I need to update a graph made in javascript without stopping running PHP, I simplified the problem in the code below, if I can make the code below run, I can achieve the goal. If someone knows how to accomplish this goal otherwise please give me a help!

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><scripttype="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 for $ i = 5, the graph is shown, "MAS", if While (true) is maintained, then the graph will never be shown!

Thank you for your patience!

    
asked by anonymous 29.05.2016 / 06:19

1 answer

1

Make an Ajax request for the '* .php' file via JavaScript. To request there are 2 native objects: XMLHttpRequest and ActiveXObject .

The ActiveXObject is supported by IE

29.05.2016 / 12:44