I am making a website that charts measurements from a SQL database using a 3rd-party JS, but I am having trouble getting values from PHP. I have read this How to pass variables and data from PHP to JavaScript? and https://www.w3schools.com/php/php_ajax_php.asp. But the values are not being fetches and I cannot find error.
Here is my HTML and JS:
var series = new TimeSeries();
var h = 1;
setInterval(function(){
document.getElementById("test").innerHTML = h;
series.append(new Date().getTime(), n);
}, 400);
AjaxFun();
function createTimeline() {
var chart = new SmoothieChart();
chart.addTimeSeries(series, { strokeStyle: 'rgba(0, 255, 255, 1)', fillStyle: 'rgba(0, 255, 255, 0.2)', lineWidth: 4 });
chart.streamTo(document.getElementById("chart"), 500);
}
function AjaxFun(){
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
h = this.responseText;
}
};
xmlhttp.open("GET", "Read.php?q="+str, true);
xmlhttp.send();}</script>
Here is my PHP:
<?php
//connect
$conn = mysql_connect('localhost', 'root', 'password', 'name');
if(!$conn){
die ('Error');
mysql_close($conn);
echo ('die');
exit;
}
$db = mysql_select_db("data", $conn);
if(!$db){
die('fail');
mysql_close($conn);
exit;
}
//get
$find = "SELECT' 'height' FROM 'data' ORDER BY 'time' DESC LIMIT 1";
$query = mysql_query($find, $conn);
if($query){
echo($query);
mysql_close($conn);
exit;
} else{
echo ('Error');
mysql_close($conn);
exit;
}
?>
Thanks for your help. I am still new with AJAX and PHP