ster ster - 3 months ago 17
Javascript Question

import values from PHP with JS for chart column

I have this code in javascript for chart column:



$(function () {
$('#grafic_column').highcharts({
chart: {
type: 'column'
},
series: [{
name: 'Population',
data: [
['Shanghai', 23.7],
['Lagos', 16.1],
['Istanbul', 14.2]
]
}]
});
});





And this cod in php:



<?php
$ar = array(
['Shanghai', 23.7],
['Lagos', 16.1],
['Istanbul', 14.2]
);
echo json_encode($ar);
?>





How do import the values results from php in
series.data:
? Thank you!

Answer

Assuming your PHP file is accessible with a server, you can just perform $.get to make an AJAX request to the file serving the data:

$(function () {
    $.get('/yourphpfile.php').then(function(data) {
        $('#grafic_column').highcharts({
            chart: {
                type: 'column'
            },
            series: [{
                name: 'Population',
                data: data
            }]
        });
    })
});