Jump to content

Passing variable to php script for google charts


Kris1988

Recommended Posts

Hi

 

I am trying to create a google chart from data in query.php and using the google api to load it. Everything works until I want to change the MetalSourceID from a drop select box. 

 

PHP CODE FOR DROP DOWN BOX:

 

<form>
<select name="users" onchange="showUser(this.value);drawChart();">
<option value=""> Select a Metal: </option>
<?php
$query = "SELECT TOP(31) tblMetalPrice.MetalSourceID, tblMetalSource.MetalSourceName from tblMetalPrice INNER JOIN tblMetalSource ON tblMetalPrice.MetalSourceID=tblMetalSource.MetalSourceID ORDER BY tblMetalPrice.DateCreated DESC ";
$result = sqlsrv_query( $conn, $query);
while( $row = sqlsrv_fetch_object ($result)) {
echo "<option value='".$row->MetalSourceID ."'>". $row->MetalSourceName ."</option>";
}
sqlsrv_close( $conn);
?>
</select>
</form>

This works fine and generates all the correct values. One part of this changes contents of a table which works fine. But I also echo the MetalSourceID into the javascript for the google api, JS script below:

 

<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);


function drawChart() {
var jsonData = $.ajax({
url: "query.php",
dataType:"json",
async: false,
data: { 'MetalSourceID' : <?php $q = intval($_GET['q']); echo $q; ?> }
}).responseText;


var data = new google.visualization.DataTable(jsonData);


var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data); 
}


</script>

This then runs the query.php script and returns the google line chart, a copy of the query.php script is below:

 

$query ="SELECT TOP(30)tblMetalPrice.DateCreated, tblMetalPrice.UnitPrice, tblMetalPrice.HighUnitPrice FROM tblMetalPrice WHERE tblMetalPrice.MetalSourceID = '" .$q. "' ORDER BY tblMetalPrice.DateCreated DESC";

    $array['cols'][] = array(
            'id' => 'Date',
            'label' => 'Date',
            'type' => 'string'
            );
    $array['cols'][] = array(
            'id' => 'Price',
            'label' => 'UnitPrice',
            'type' => 'number'
            );
$array['cols'][] = array(
            'id' => 'Price',
            'label' => 'HighUnitPrice',
            'type' => 'number'
            );
            
    $result = sqlsrv_query($conn, $query);
    while($row = sqlsrv_fetch_object($result)){ 
        $array['rows'][] = array (
                        'c' =>array(
                                array ('v' => $row->DateCreated->format('d-m-Y')),
                                array ('v' => $row->UnitPrice),
                                array ('v' => $row->HighUnitPrice),
                            )
                        );
    }
    return $array;
}

print json_encode(graphdata());

sqlsrv_close( $conn);

The scripts works when the $q variable is static, how can I make it that when some changes the drop down box that it refreshed the data in the graph and displays the new data?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.