Fernando Issler Fernando Issler - 6 months ago 18
PHP Question

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

Answer

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