I am working on a project where the user select an item, the server (php) retrieves the cell codes (and the geographic coordinates of these cells) connected to that item, and should create the Json file that will be used by the javascript in the client side to update the map.
I think I understood that it is mandatory to create the json file that google map api will use to update the map.
So how should i handle this in the server file? I mean, do i have to create a json file with random filename each time a request is done and then after return the json file to the client delete it or are there any different and better approach?
Have You heard about Headers? Especially "Content-type" may be interesting for You ;)
Pass AJAX request and get response data in JSON using mysql query. Feed response data to chart function.
In short, you need to get array values -> convert into JSON and give those values to google chart irrespective whether you need mysql or not. Modify code according to your requirement.
<script>
drawLineChart('<?php echo strtolower($chartType); ?>');
</script>
function drawLineChart(chartType, chartTitle) {
google.charts.setOnLoadCallback(lineChartData);
function lineChartData() {
var lineChartJsonData = $.ajax({
type: 'GET',
url: "<?php echo $server_script_path; ?>",
data: { id1: chartType, id2: "Chart" },
dataType:"json",
async: false,
beforeSend: function() {
},
success: function(data) {
},
}).responseText;
var options;
options = {
title: chartTitle,
width: '390',
height: '300',
vAxis: {title: '<title>'},
};
var data = new google.visualization.arrayToDataTable(($.parseJSON(lineChartJsonData)));
var chart = new google.visualization.LineChart(document.getElementById(chartType));
chart.draw(data, options);
}
}
if ($_GET['id1'] != "" && $_GET['id2'] == "Chart") {
// Chart type
$chartType = explode('-', $_GET['id1'])[1];
$sql = "<mysql query>";
$result = mysqli_query($conn, $sql);
$table = array();
$rows = array();
$rows[] = array('<column1>', '<column2>');
while($row = mysqli_fetch_array($result)) {
$rows[] = array('<rows>', '<rows>');
}
$table = $rows;
echo json_encode($table);
}
Please use geographic coordinates type of chart according to your need.