Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my php file it contains three arrays and I am passing these three arrays to external JavaScript file called ss.js to draw pie chart. If I directly declared array in external JavaScript file it shows the pie chart but when I passing array from php it simply showing nothing. I tried but I can't figure out the error part.

code.php

<?php 
  $a = array("Apple","Orange","Grape");
  $dataArray = array("Task","Hours Per Day");
  $arr1 = array("Work","Eat","Commute","Watch TV","Sleep");
  $arr2 = array(11,2,2,2,7);
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">
  var jArray =<?php echo json_encode($dataArray); ?>;
  var jArray1 =<?php echo json_encode($arr1); ?>;
  var jArray2 =<?php echo json_encode($arr2); ?>;
</script>

<script type="text/javascript" src="ss.js">
</script>
</head>
<body>
   <div id="piechart" style="width: 900px; height: 500px;"></div>
</body>
</html>

ss.js

google.load("visualization", "1", {
  packages: ["corechart"]
});

function drawData() {
  for (var n = 0; n < jArray2.length; n++) {
    jArray.push([jArray1[n], parseInt(jArray2[n])]);
  }
  var data = new google.visualization.arrayToDataTable(jArray);
  var options = {
    title: 'My Daily Activities'
  };
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data, options);
}
google.setOnLoadCallback(drawData);
share|improve this question
    
Why can't you use something native to PHP? –  tyteen4a03 9 hours ago
    
i dont know about that... –  Vijayakumar M 9 hours ago
    
A PHP array is not the same as a JavaScript array. You're also not using an array, you're using json. Json is not an array. I suggest reading some "how to" tutorials online –  user3036342 9 hours ago
    
@VijayakumarM As far as I know, a php array object is not the same as a javascript array object. did you take a view into the html source code output? –  reporter 9 hours ago
    
Issue you are facing is not about passing data from PHP to JS but its more how you create the JSON. Try the solutions in these questions stackoverflow.com/questions/23168129/… stackoverflow.com/questions/21333727/… –  Jose Antony 9 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.