1

I am using ajax to connect to a PHP script and then retrieve the graph coordinates needed for the jqplot jQuery graphing library.

The problem is that I am having difficulties constructing a proper PHP array that can then be converted into a jQuery array that jqplot can read.

Here is the code that retrieves the array from the PHP file:

$.ajax({
    type: 'POST',
    dataType: "json",
    url: "behind_curtains.php",
    data: {
        monthSelected: month_option_selected
    },
    success: function (data) {
        var stored_data = data;
        alert(stored_data);
    }
});
return stored_data;
}

Here is the code that creates the jqplot

jQuery.jqplot('chartdiv-data', [], {
    title: 'Plot With Options',
    dataRenderer: stored_data,
    axesDefaults: {
        labelRenderer: jQuery.jqplot.CanvasAxisLabelRenderer
    },
    axes: {
        xaxis: {
            label: "Day",
        },
        yaxis: {
            label: "data"
        }
    }
});

If you could please help me create the proper data array in the behind_curtains.php file it would be great!

Edit 1 the graphing coordinates array should be in the form of:

[[[1,2],[3,5],[5,13]]]

and basically I need to somehow write php code that can output an array that stores data in that form.

Thanks

Solution:

*behind_curtains.php*

I put together an array that simply generates a string in the form of [[1,2],[3,5],[5,13]]. I then stored the string in a variable and echoed that variable in the form of:

echo json_encode($stored_data);

On the user side of things here is how my code looked like:

<script type="text/javascript">
$("document").ready(function() {
        $.ajax({
            type: 'POST',
            dataType:"json",
            url: "behind_curtains.php",
            data: {
                monthSelected: month_option_selected},
            success: function(data){                    
            var stored_data = eval(data) ;  
/* generate graph! */
$.jqplot('chartdiv-weight', [stored_data], {
 title: month, 
axesDefaults: {
    labelRenderer: jQuery.jqplot.CanvasAxisLabelRenderer
  },
  axes: {
    xaxis: {
      label: "Day",
    },
    yaxis: {
      label: "data"
    }
  }   
});
        }
        });
    }

}});</script>

I hope that helps, if no, whoever is interested please let me know.

2
  • just a tip: you can't "return" data after an AJAX call. stored_data will be blank when that function returns it. Commented May 21, 2012 at 16:24
  • Thanks so much, I got rid of it:) (I am very new to ajax) Commented May 21, 2012 at 16:27

1 Answer 1

1

You can return the stored_data but you need to have it declared before the AJAX call, like shown in the bottom most example, here. You must also use: async: false otherwise you cannot return your data in this fashion. For the same reason if you like you could use getJSON() instead.

As it goes to formatting your retrieved data a similar issue I answered here. You need to build your array accordingly to represent your data.

If you can show how exactly does your JSON look like then I could give you a more precise answer.

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

2 Comments

Thank you very much Boro for your answer, I ended up figuring it out!
@Dmitri it would be very good if you could share with others what was your solution. I see there is at least one person who favors your question it might be of use to him/her to know your solution.

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.