0
votes

I'm using this script in my file to shown error "Uncaught SyntaxError: Unexpected token '<'" in console. How I fix it? Error is shown at the PHP tag which is defined in the script.

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>  
           <script type="text/javascript">  
           google.charts.load('current', {'packages':['corechart']});  
           google.charts.setOnLoadCallback(drawChart);  
           function drawChart()  
           {  
                var data = google.visualization.arrayToDataTable([  
                          ['Car', 'Number'], 
                          <?php  
                          while($row = mysqli_fetch_array($result))  
                          {  
                               echo "['".$row["car"]."', ".$row["number"]."],";  
                          }  
                          ?>  
                     ]);  
                var options = {  
                      title: 'Percentage of branded Car Services',  
                      //is3D:true,  
                      pieHole: 0.4  
                     };  
                var chart = new google.visualization.PieChart(document.getElementById('piechart'));  
                chart.draw(data, options);  
           }  
           </script> 
2
Your code it is on a js page or php page? I hope it is a php file. - Cristian V

2 Answers

0
votes

Your while,

while($row = mysqli_fetch_array($result))  
{  
   echo "['".$row["car"]."', ".$row["number"]."],";  
} 

the last echo will print a "," and this can generate your problem.

I recomand to build an array and then use echo with an implode().

0
votes

You cannot place php inside javascript however you can echo javascript in php.