Graphic Passing php data to js

Asked

Viewed 216 times

0

Hello,

I have a php file with a div calling the js file, but I need the js file to read the php data

in arquvio php:

    <div id="chart_div" style="width: 100%; height: 500px;"></div>

in the js file:

    function mainChart(){

  var color1 = App.color.primary;
  var color2 = tinycolor( App.color.primary ).lighten( 13 ).toString();
  var color3 = tinycolor( App.color.primary ).lighten( 20 ).toString();

  var data = [
    [1, 35],
    [2, 60],
    [3, 40],
    [4, 65],
    [5, 45],
    [6, 75],
    [7, 35],
    [8, 40],
    [9, 60]
  ];

instead of this fixed array I have to fetch this data in the Database would look lake like this:

function mainChart(){

    var color1 = App.color.primary;
    var color2 = tinycolor( App.color.primary ).lighten( 13 ).toString();
    var color3 = tinycolor( App.color.primary ).lighten( 20 ).toString();

    var data = [
        <?php
        $i = '0';
        foreach ($ConsultasCanceladas as $canceladas) {
            $i = ++$i;
            echo "['" . $i . "', " . $canceladas->dt_consulta . "],";
        }
        ?>
    ];

What happens is that the js file does not accept php

  • Put this mainChart function inside the PHP file, or use AJAX to get this data.

1 answer

0

You can get the data for your Javascript using AJAX, or you can choose to put some classes in your HTML and pick it up via Jquery or even pure Javascript.

Examples:

  • Using Jquery: $(". elements")
  • Using Pure JS: var elements = Document.getElementsByClassName("elements");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.