Senthilkumar's post in Pass the PHP vaiable to js file using ajax script was marked as the answer
April 9
I changed the code as per your instructions. Now it is working.
<?PHP
include("../connection.php");
$Month = $_POST["Month"];
$Region = $_POST["Region"];
$Equipment = $_POST["Equipment"];
if ($Equipment == '') {
$EquipCondition = '';
} else {
$EquipCondition = 'AND ModelGroup = ' .$Equipment.'';
}
if ($Region == '') {
$Mc = "SELECT Region, CuTotal, CuAssigned FROM
( SELECT count(distinct a.CustomerID) as CuTotal, c.regnName as Region FROM sbms.machinemaster as a
inner join sbms.branch as b on b.branchcode = a.BranchCode
inner join sbms.region as c on c.regnID = b.regnID
WHERE a.MachineStatus = 'A' $EquipCondition AND c.regnID !='5' group by c.regnID) total
LEFT JOIN (Select count(a.RowID) as CuAssigned , c.regnName as Region FROM sbms.customerdata as a
inner join sbms.branch as b on b.branchID = a.Branch
inner join sbms.region as c on c.regnID = b.regnID
inner join sbms.machinemaster as d on d.id = a.RowID
WHERE a.Month = '$Month' $EquipCondition and d.MachineStatus='A' AND a.VisitType !='No Due' AND a.Status ='1'
group by c.regnID order by c.regnID ASC ) assigned USING (Region)";
$Mcresult = mysqli_query($conn, $Mc);
$McNoRow = mysqli_num_rows($Mcresult);
$return_arr[] = array('Region', 'Active Customer', 'Visited');
while ($Mcrow = mysqli_fetch_array($Mcresult)) {
$CustRegion = $Mcrow['Region'];
$CusTotal = $Mcrow['CuTotal'];
$CusAssigned = $Mcrow['CuAssigned'];
$return_arr1[] = array(
$CustRegion
);
$return_arr2[] = array(
$CusTotal
);
$return_arr3[] = array(
$CusAssigned
);
}
echo json_encode(array("CustRegion" => $return_arr1, "CusTotal" => $return_arr2, "CusAssigned" => $return_arr3));
}
if ($Region !== '') {
$Mc = "SELECT Branch, McTotal, McAssigned FROM
( SELECT count(distinct a.CustomerID) as McTotal, b.branchName as Branch FROM sbms.machinemaster as a
inner join sbms.branch as b on b.branchcode = a.BranchCode
inner join sbms.region as c on c.regnID = b.regnID
WHERE a.MachineStatus = 'A' $EquipCondition AND c.regnID ='$Region' group by b.branchcode) total
LEFT JOIN (Select count(a.RowID) as McAssigned , b.branchName as Branch FROM sbms.customerdata as a
inner join sbms.branch as b on b.branchID = a.Branch
inner join sbms.region as c on c.regnID = b.regnID
inner join sbms.machinemaster as d on d.id = a.RowID
WHERE a.Month = '$Month' $EquipCondition and d.MachineStatus='A' AND b.regnID = '$Region' AND a.VisitType !='No Due' AND a.Status ='1'
group by b.branchID ) assigned USING (Branch)";
$Mcresult = mysqli_query($conn, $Mc);
$McNoRow = mysqli_num_rows($Mcresult);
$return_arr[] = array('Region', 'Active Machine', 'Procuction Data Collected');
while ($Mcrow = mysqli_fetch_array($Mcresult)) {
$CustRegion = $Mcrow['Branch'];
$CusTotal = $Mcrow['McTotal'];
$CusAssigned = $Mcrow['McAssigned'];
$return_arr1[] = array(
$CustRegion
);
$return_arr2[] = array(
$CusTotal
);
$return_arr3[] = array(
$CusAssigned
);
}
echo json_encode(array("CustRegion" => $return_arr1, "CusTotal" => $return_arr2, "CusAssigned" => $return_arr3));
}
?>
<script type="text/javascript">
$(document).ready(function () {
ProductionFilter();
});
$(document).on('change', '.filter', function () {
ProductionFilter();
});
function ProductionFilter() {
var Month = document.getElementById("Month").value;
var Region = document.getElementById("Region").value;
var Branch = document.getElementById("Branch").value;
var Equipment = document.getElementById("Equipment").value;
$.ajax({
url: 'Customer_Graph_Filter.php',
type: 'POST',
data: 'Month=' + Month + '&Region=' + Region + '&Branch=' + Branch + '&Equipment=' + Equipment,
dataType: 'JSON',
success: function (response) {
var CustomerRegion = response.CustRegion;
var CustomerAssigned = response.CusAssigned;
var CustomerTotal = response.CusTotal;
var barChartData = {
labels: CustomerRegion,
datasets: [{
label: 'Total Customers',
backgroundColor: 'rgba(0, 158, 251, 0.5)',
borderColor: 'rgba(0, 158, 251, 1)',
borderWidth: 1,
data: CustomerTotal
}, {
label: 'Data Collected',
backgroundColor: 'rgba(255, 188, 53, 0.5)',
borderColor: 'rgba(255, 188, 53, 1)',
borderWidth: 1,
data: CustomerAssigned
}]
};
var myoption = {
responsive: true,
tooltips: {
enabled: true
},
hover: {
animationDuration: 1
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
}
}],
xAxes: [{
ticks: {
autoSkip: false
}
}]
},
animation: {
duration: 1,
onComplete: function () {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.textAlign = 'center';
ctx.fillStyle = "rgba(0, 0, 0, 1)";
ctx.textBaseline = 'bottom';
this.data.datasets.forEach(function (dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function (bar, index) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y + 1);
});
});
}
},
maintainAspectRatio: true
};
var ctx = document.getElementById('CustomerBarGraph').getContext('2d');
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: myoption,
});
}
});
}
</script>
Thanks for your support