2

I have a chart js on my website! I'm having trouble getting grab data from an external PHP page. Can someone help me?

var result = [];

        $.ajax({
                url: 'x.php',
                dataType: 'text',
                async: false,
                success:  function(data) {
                        items = data
                }
        });

     result = items


        var dataProcessosAtivos = [
                result
        ];

The original variable is as follows:

var dataProcessosAtivos = [
        [0, 4],
        [1, 8],
        [2, 0],
        [3, 0],
        [4, 0],
        [5, 0],
        [6, 0],
        [7, 0],
        [8, 0],
        [9, 0],
        [10, 0],
        [11, 0]
];

This is a x.php:

    $json = "[0,87],[1, 14],[2, 16],[3, 0],[4, 0],[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]";

    echo json_encode($json);
2
  • Did you test x.php to ensure that it actually returns JSON, the headers are correct (as per jQuery) and that it returns the array as you expect? Commented May 11, 2016 at 3:04
  • This is a x.php --- $json = "[0,87],[1, 14],[2, 16],[3, 0],[4, 0],[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]"; echo json_encode($json); Commented May 11, 2016 at 3:29

2 Answers 2

1

Change php file content to follows. And if its an existing chart in DOM make sure chart re-draw itself after you set result.

// This tells the bellow content is JSON
header ('content-type: application/json');

// Actual PHP array
$json = [[0,87],[1, 14],[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]];

// Printing JSON string
echo json_encode($json);
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks John Fonseka!!!

My code looks like this:

$.ajax({
        url: 'x.php',
        dataType: 'json',
        async: false,
        success:  function(data) {
                items = data
        }
});

var dataProcessosAtivos = items;

and the x.php

header ('content-type: application/json');
$json = [[0,87],[1, 14],[5, 0],[6, 0],[7, 0],[8, 0],[9, 0],[10, 0],[11, 0]];
echo json_encode($json);

I hope it helps others. Thanks!

Comments

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.