0

I am using Jquery Flot Chart for plotting charts. To pass the data to chart,I am getting data from the database, and then I need to pass that php array into javascript array. I am using json_encode($array),but it is not working.

     $row=array();
      while($res=mysql_fetch_array($exequery))
     {
        $row[]=$res[0];
     }

Now,in the same file(without using ajax), I want to pass this php array to javascript array,which then will be used to plot chart.

    <script src="jquery-1.8.3.min.js"></script>
     <script src="jquer.flot.min.js"></script>     
     <script type ="text/javascript">
     var dataArray=<?php echo json_encode($row)?>;

When I am using this, chart is getting disappeared and alert(dataArray) is also showing nothing. Please help.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
uddipana
  • 188
  • 2
  • 19

1 Answers1

0

Json parse using jquery:

var dataArray=jQuery.parseJSON('<?php echo json_encode($row)?>');

Open this link:- http://api.jquery.com/jquery.parsejson/

  • 1
    The quotes and `parseJSON()` aren't really necessary when outputting JSON directly into JavaScript code. Provided it's output where an *Expression* is expected, such as to the right of an assignment operator, the string of JSON will be parsed as valid JavaScript, using the keywords, literals, and initializers that inspired JSON's syntax. – Jonathan Lonowski Jul 16 '15 at 06:03