0

I need to pass a parameter for make a query in Ajax file (query.php) for render json data for my flot chart.

I have a table when I click a row I take an ID necessary for the query

$("#dt-tabella tbody").on('click', 'tr', function () {
            var data = table.row( this ).data();
             $('#titoloGraf').text("Grafici : " + data[0]);
             $.ajax({
     url: "include/ajax/query.php",
     type: "POST",
     dataType: "json",
     success: onDataReceived
    });
        }); 
...
function onDataReceived(series) {
     data = [ series ];
     $.plot("#Grafico", data, options);
    }
<div class="flot-chart">
<div class="flot-chart-content" id="Grafico"></div>
</div>

On my query.php I need to take my parameter for the query (data[0]):

$tmp = filter_input(INPUT_GET, 'ID_param');
..somequery..

and my test result is something like

echo '{
"label": "'.$tmp.' (EU27)",
"data": [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3],
[2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]}';

Obviously at the end my var $tmp is not plot cause I don't know how to pass ID_param.

Any Idea is appreciated

MadCat
  • 113
  • 1
  • 1
  • 13

2 Answers2

1

Here is how you can send data to you PHP page query.php using AJAX.

http://www.w3schools.com/jquery/jquery_ajax_get_post.asp

And here is how you can get that data in your query.php

if($_POST){
$param = $_POST['param_name']);
}
Nikunj Sardhara
  • 638
  • 1
  • 5
  • 20
1

I found this solution...

$("#dt-tabella tbody").on('click', 'tr', function () {
  $.ajax({ 
            type: 'POST', 
            url: 'include/ajax/query.php', 
            data: { data: data[0] }, 
            dataType: 'json',
            success: function (data) { 
                series = [ data ];
        $.plot("#Grafico", series, options);
            }});
};

my problem was use the response and use "data" for plot graph, the idea come from how to parse json data with jquery / javascript?

Community
  • 1
  • 1
MadCat
  • 113
  • 1
  • 1
  • 13