0

Impossible to get out in this passage of variable I tried many things ... It is however a normally not complicated thing ...

I would like to pass a variable: SerialNumberJS which is in the JS (which I retrieve via the URL: getUrlParameter function) to the PHP file! This variable will then be used in the PHP to make a base query. Then go back to the JS to display a graph!

Help please :)

$(document).ready(function(){

    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
    };

    var SerialNumberJS = getUrlParameter('SerialNumber'); 
    console.log(SerialNumberJS);


    $.ajax({
        url : "graphdata.php",
        type : "GET",
        success : function(data){
            console.log(data);

            var heure = [];
            var payload = [];

            for(var i in data) {
                heure.push("Heure " + data[i].heure);
                payload.push(data[i].payload);
            }

            var chartdata = {
                labels: heure,
                datasets: [
                    {
                        label: "payload",
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: "rgba(59, 89, 152, 0.75)",
                        borderColor: "rgba(59, 89, 152, 1)",
                        pointHoverBackgroundColor: "rgba(59, 89, 152, 1)",
                        pointHoverBorderColor: "rgba(59, 89, 152, 1)",
                        data: payload
                    }
                ]
            };

            var ctx = $("#mycanvas");

            var LineGraph = new Chart(ctx, {
                type: 'line',
                data: chartdata
            });
        },
        error : function(data) {

        }
    });
});
<?php

//setting header to json
header('Content-Type: application/json');

//database
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '****');
define('DB_NAME', 'delta');

//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

if(!$mysqli){
    die("Connection failed: " . $mysqli->error);
}

$SerialNumber = "00000000010";

//query to get data from the table
$query = 'SELECT heure, payload FROM reception WHERE Serial_number = "'.$SerialNumber.'" ORDER BY date DESC LIMIT 10';

//execute query
$result = $mysqli->query($query);

//loop through the returned data
$data = array();
foreach ($result as $row) {
    $data[] = $row;
}
//free memory associated with result
$result->close();

//close connection
$mysqli->close();

//now print the data
print json_encode($data);
?>
3
  • What you get in console?? Commented Sep 2, 2017 at 13:12
  • With this code, we have the graph and the SQL query works because $ SerialNumber is defined in the PHP code. But I tried to add a GET or a POST to send SerialNumber, but each time the request is not done any more after ... Commented Sep 2, 2017 at 13:22
  • you can see my changes in my answers. I still do not have the expected result :) Commented Sep 2, 2017 at 14:14

1 Answer 1

1

To get the serial number in PHP file you need to pass it in ajax request's data element:

$.ajax({
        url : "graphdata.php",
        type : "GET",
        data: {
            'SerialNumberJS' : SerialNumberJS // add other if needed
        },
        success : function(data){
            // your code....
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

you can see my changes in my answers. I still do not have the expected result :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.