Fernando Issler
Fernando Issler

Reputation: 43

Chart with external ajax data PHP

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);

Upvotes: 2

Views: 77

Answers (2)

Fernando Issler
Fernando Issler

Reputation: 43

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!

Upvotes: 1

John Fonseka
John Fonseka

Reputation: 795

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);

Upvotes: 1

Related Questions