0

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Apr 12 '17 at 20:15
  • 1
    And I am pretty sure you are mixing the `mysql_` and `mysqli_` API's – RiggsFolly Apr 12 '17 at 20:18
  • http://php.net/manual/en/function.mysqli-connect.php – RiggsFolly Apr 12 '17 at 20:19
  • all the code that exists after a call to die() will not run. That function kills the script. – victor Apr 12 '17 at 20:20
  • @RiggsFolly That was just a typo. – Haley Alexander Apr 12 '17 at 20:25
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Apr 12 '17 at 20:25
  • In that case you are more confused than I originally thought. `mysql_connect()` only has 3 parameters and you have 4 and should be followed by a `mysql_select_db()` So lots of typo's exist in here – RiggsFolly Apr 12 '17 at 20:28
  • Also any code after a `die()` is totally superfluous as the script is DEAD – RiggsFolly Apr 12 '17 at 20:30

0 Answers0