Change external Javascript value to PHP in foreach loop

How do I change the value of a js variable in a PHP foreach loop?

js must be external.

This is the part index.php

here I want to get the output of $ prozent in javascript, I dont know how to get PHP variable to execute js every time in a loop, js is a diagram. I am getting a diagram in a loop in echo'<div class="chart-wrapper">

with this js diagram.

<?php
                $sql = //SQL query
                $res = mysql_query($sql);
                $i = 0;
                $survey = array();
                 while ($row = mysql_fetch_assoc($res)) {
                    $survey[$i]['label'] = $row['Label'];
                    $i++;
                }

                    foreach ($survey as $survey) {    
                    echo '<div class="col-md-4">';
                    echo '<div class="front face">';                

            echo "<h3> ".$survey['label']."<br>";
            $sql = //SQL Query
            $res = mysql_query($sql) ;
            $sql = //SQL Query
                $resSum = mysql_query($sql) ;
            while ($row = mysql_fetch_array($resSum)) {
                $survey['ausgabe'] = $row['sum(a.answer)'];
                 }


                $anzahlreihen = mysql_num_rows($res);
                if ($anzahlreihen > 0) {    
                $prozent = $anzahlreihen * $survey['ausgabe'];
                   //bunch of ifelse statements
              //This is the variable i want to get in the js    
            echo (round( $prozent, 2));
                    echo '</font>';
                }
                }
            echo '</div>';
            echo '<div class="back face center">';
            echo'<div class="chart-wrapper">
        }

      

+3


source to share


1 answer


There are two ways PHP and Javascript interact.

Use AJAX to request a PHP script that returns JSON or has PHP output in the following HTML example;



<script type="text/javascript">
var variable = value;
</script>

      

+1


source







All Articles