0

I want to get a simple data (a number in fact) from a php file (via ajax or json) into a vitiable in jQuery.

I have tried a lot of Ajax and jSon method... no success.

I got this:

while (data.length < totalPoints)
{
    var prev = data.length > 0 ? data[data.length - 1] : 60;
    var y = prev + Math.random() * 10 - 5;
    if (y < 0)
        y = 0;
    if (y > 60)
        y = 60;
    data.push(y);
}

I want to replace the "Math.random() * 10 - 5" with a number in a php file that have a simple number in it from MySql.

This loop will go on 60 times for the first time an only once every 15 sec.

This is not working. The php file is call and return the number but the 'fromPhp' is empty.

var data = [], totalPoints = 60;

function getRandomData() {
    if (data.length > 0)
        data = data.slice(1);

    var fromPhp = '';
    var y;
    while (data.length < totalPoints) {
        var prev = data.length > 0 ? data[data.length - 1] : 60;
        $.get('assets/get_number.php', function (theNumber) {
            fromPhp = theNumber;
        });
        y = prev + fromPhp;
        if (y < 0)
        y = 0;
        if (y > 60)
        y = 60;
        data.push(y);
    }

    var res = [];
    for (var i = 0; i < data.length; ++i)
    res.push([i, data[i]])
    return res;

}

Actually all the number (y) are '60'

  • 2
    what were the problems encountered with ajax? Should really be posting code that wasn't working for you to try to remedy – charlietfl Sep 16 '14 at 20:23
  • 1
    Do you really need AJAX for this, or is this number you want to insert into the javascript function known at page load time and can simply be inserted via `echo` statement concatenation with rest of js script? – Mike Brant Sep 16 '14 at 21:04
  • It don't really need Ajax. I just want to import this number from a php (actually echo in the php) file into the loop in the js file. – Richard St-Pierre Sep 16 '14 at 21:23
  • the ajax call is asynchronous so the `y = prev + fromPhp;` will run regardless if the ajax already completed which means it will most likely run before `fromPhp = theNumber;` runs. you will need to call the function from the ajax's success function and use recursion instead of while cycle. Anyway this whole question stinks with bad practice – Kyborek Sep 16 '14 at 21:39

1 Answers1

1

try this code and check console. It will make you understand why the code doesn't work:

function getRandomData() {
if (data.length > 0)
    data = data.slice(1);

var fromPhp = '';
var y;
while (data.length < totalPoints) {
    var prev = data.length > 0 ? data[data.length - 1] : 60;
    $.get('assets/get_number.php', function (theNumber) {
        fromPhp = theNumber;
        console.log("Number loaded from php");
    });
    y = prev + fromPhp;
    console.log("Number added to the Y");
    if (y < 0)
    y = 0;
    if (y > 60)
    y = 60;
    data.push(y);
}

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

I belive this answer could enlighten you about how to get data from php into javascript: How to pass variables and data from PHP to JavaScript? I don't know exactly what goal do you want to achieve but i would suggest to use second or third option of accepted answer (Echo the data directly to JavaScript)

Community
  • 1
  • 1
Kyborek
  • 1,519
  • 11
  • 20