I'm running into an issue and I need help. I'm able to show a horizontal column chart from the Google API with dynamic data from a MySQL table on page load. But when I click a button and the data is being posted to the page, the chart does not print on the HTML even tho I'm able to see in the response tab in the console that the script has been generated correctly. However, when I look in the HTML in the inspector, I can see that the JavaScript appears greyed-out on the page as if it was commented-out. I've been scratching my head for hours. Serverside I'm using PHP. Clientside I'm using Javascript/jQuery + HTML. How can I fix this? If you have a better way to generate a chart from PHP to HTML, I'm opened to suggestions.
graph.html
<div id="chart_div1"></div>
jquery
$(document).on('click', '.view_sales_year_btn', function () {
//collect variables
var sales_year = $("#sales_year").find(":selected").val();
var valuesf = "user_id=" + encodeURIComponent(user_id)
+ "&sales_year=" + encodeURIComponent(sales_year);
if (sales_year !== '' || sales_year !== null && valuesf !== '') {
$.ajax({
type: "POST",
url: "graph.php",
dataType: 'json',
data: valuesf,
success: function (data) {
//DISPLAY SALES GRAPH'
$('#chart_div1').html(data.sales_graph);
}
});
}
});
graph.php
//collect variables
$user_id = mysqli_real_escape_string($conn,$_POST['user_id']);
if(isset($_POST['sales_year'])){
//post year
$sales_year=mysqli_real_escape_string($conn,$_POST['sales_year']);
}else{
$sales_year = date('Y');
}
//set title for graph
$graph_title ='My Sales '.$sales_year;
//cleaning status
$c_status = 'Booked';
//select all completed cleanings from user_schedule
$user_schedule = $conn->prepare('SELECT cleaner_id,
MONTH(cleaning_date) AS sales_month,
YEAR(cleaning_date) AS sales_year,
SUM(cleaning_price) AS total_sales
FROM user_schedule
WHERE YEAR(cleaning_date)=?
AND cleaner_id=?
AND cleaning_status=?
GROUP BY DATE_FORMAT(cleaning_date, "%m-%Y")');
$user_schedule->bind_param('sss',$sales_year,$user_id,$c_status);
$user_schedule->execute();
$user_result = $user_schedule->get_result();
//iteration
if($user_result->num_rows>0){
//declare variable
$my_sales = '';
$my_sales .='<script type="text/javascript">';
$my_sales .=" google.load('visualization', '1', {packages:['corechart']});
google.setOnLoadCallback(drawChart1);
function drawChart1(){
var data = google.visualization.arrayToDataTable([
['Month', 'Sales'],";
while($sales = $user_result->fetch_assoc()){
//get month number
$monthNum =$sales['sales_month'];
//convert monh number to month name
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$month = $dateObj->format('F'); // March
//get first 3 characters from month
$this_month = substr($month, 0, 3);
//select all completed cleanings from user_schedule
$total_month_sales = (int)$sales['total_sales']*0.35;
//echo month with total sales
$my_sales .="['".$this_month."', ".$total_month_sales."],";
}
$my_sales .= "]);
var options = {
title: '".$graph_title."',
hAxis: {title: 'Amounts may differ from Stripe', titleTextStyle: {color: 'blue'}}
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div1'));
chart.draw(data, options);
}
$(window).resize(function(){
drawChart1();
});
</script>";
}else{
$my_sales = '<h4 class="text-center alert alert-warning">You Have No Sales For '.$sales_year.' Yet.</h4>';
}
//return answer to json
$result = array('message' => 'success','sales_graph'=>$my_sales);
header('Content-Type: application/json');
echo json_encode($result);