0

how do i pass an array from php to jquery both are in same file , i just have an array named $array2 with data that will be used to make graph below code is using chartdata but i want to use variable from my php script

 <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
var chart;

var chartData = [{
    student: 5,
    marks: 0},
{
    student: 8,
    marks: 50},
{
    student: 10,
    marks: 100}];


AmCharts.ready(function() {
    // SERIAL CHART
    chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;
    chart.categoryField = "marks";
    chart.startDuration = 1;

    // AXES
    // category
    var categoryAxis = chart.categoryAxis;
    categoryAxis.labelRotation = 90;
    categoryAxis.gridPosition = "student";

    // value
    // in case you don't want to change default settings of value axis,
    // you don't need to create it, as one value axis is created automatically.
    // GRAPH
    var graph = new AmCharts.AmGraph();
    graph.valueField = "student";
    graph.balloonText = "[[category]]: [[value]]";
    graph.type = "column";

    graph.lineAlpha = 0;
    graph.fillAlphas = 8.4;
    chart.addGraph(graph);

    chart.write("chartdiv");
});
 </script>
1

4 Answers 4

1

you can use json data to pass php to jquery.

in php

$php_data = json_encode($your_php_data_in_array);

assigning to jquery

var data = <?php echo $php_data ;?>

getting the value in jquery

var chart_data_arr = json_decode(data);

now you have the data in array in jquery.

Sign up to request clarification or add additional context in comments.

Comments

0

The simplest (and not necessarily the most scure/efficient/modular/blah blah) possible way you can just dump it using json_encode

var chartData = [{
    student: 5,
    marks: 0},
{
    student: 8,
    marks: 50},
{
    student: 10,
    marks: 100}];

//Instead
var chartData=<?php echo json_encode($chartdata)?>

Alternately, you would pass it using a json action that will render the json at a certian link and use jquery to query this url and then generate the chart with that data.

2 Comments

quite didnt get ur alternate solution can u explain a bit more
ajax. hyou have a script chartdata.php which does the echoing of the json encoded data. then you make an ajax call to chartdata.php and get the data in a variable $.get("chartdata.php", function(){generateChart(data),"json"}
0

To use php data in javascript use:-

var javascriptVariable = <?php echo json_encode($php_array); ?>;

So javascriptVariable would become an object/array as per your php data.

Comments

0

Try this :

<?php 

  $your_php_array = array(.....);
?>
var chartData = <?php json_encode($your_php_array)?>;

ref: http://php.net/manual/en/function.json-encode.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.