0

I'm a having problems using AJAX to have value on the client side to draw a graph in real time with flot.

I am using one of the code examples of the flot library: the realtime.

http://www.flotcharts.org/flot/examples/realtime/

The values of this example are generated locally on the client side using javascript. But i want values from the server side, so i have to reuse the example code using ajax.

    var data = [],
        totalPoints = 10;
    var y;

    function getRandomData() {

        if (datagraph.length > 0)
            datagraph = datagraph.slice(1);

        // Do a random walk

        while (datagraph.length < totalPoints) {
              $.ajax({

                    url: 'request.php',
                    type: "POST",
                    dataType: "text",
                    cache: false,
                    success: function(data) {
                           y = data;
                           console.log("ajax: " + y);
    },

  });


            datagraph.push(y);
        }

        // Zip the generated y values with the x values

        var res = [];
        for (var i = 0; i < datagraph.length; ++i) {
            res.push([i, datagraph[i]])
        }

        return res;
    }

I have to pass the returned value from the request.php to the variable y of the function getradomData() inside of javascript on the html code above.

Note: the request.php calls a function: getVoltage() that returns a value. I am echo that value on the request.php. plus, the console.log on success does not return anything.

Whats wrong on Ajax part?

Ricardo

edit: redirect.php

<?php
include('database.php');
echo getVoltage();
?>

The function getVoltage return a value like 176, and its working. I can see the value if i echo the value. getVoltage() is a function that connect to a mysql database and read the most recent value of that database.

Edit: It's working properly now after transformed the echoes string into a int.

0 Answers0