1

I have an array $results and I need to use it inside my javascript part of the code. I tried json_encode() but it did not work..

Here is the code

<?php
    //...
    include realpath($_SERVER['DOCUMENT_ROOT'] . '/Classes/Controllers/ReportController.php');
    $vaccRep= ReportController::getVacRep();

    include realpath($_SERVER['DOCUMENT_ROOT'] . '/Classes/Controllers/VaccineController.php');
    $names=VaccineController::getVCName();
?>

<canvas id="bar" height="195" width="250" style="width: 250px; height: 195px;"></canvas>
<script>
    //THIS IS THE PART THAT WILL BE THE OTPUT OF THE QUERY

    var barChartData = {
        labels: <?php$obj=json_encode($names); var_dump($obj);?>,
        datasets: [{
            highlightFill: "#45668e",
            highlightStroke: "#45668e",
            fillColor : "#1ABC9C",
            strokeColor : "#1ABC9C",
            data: <?php $obj=json_encode($vaccRep); var_dump($obj);?>
        }]
    };


    new Chart(document.getElementById("bar").getContext("2d")).Bar(barChartData);
    <?php $obj=json_encode($vaccRep); var_dump($obj);?>
</script>
3
  • 1
    Share the code you've tried. Commented May 14, 2016 at 12:36
  • you can json_encode your array in php and use it in javascript. Commented May 14, 2016 at 12:37
  • Your problem solved ? Commented May 16, 2016 at 5:08

4 Answers 4

2

Simply echo the php array in js with json_encode(), like this :

$phpArray = array('name'=>'mani','email'=>'[email protected]','mobile'=>'123467890');

PHP array looks like this

Array
(
    [name] => mani
    [email] => [email protected]
    [mobile] => 123467890
)

<script>
var jsArray = [];
var jsArray = <?php echo json_encode($phpArray);?>;
console.log(jsArray);
</script>

You will get this in your console

 Object { name="mani",  email="[email protected]",  mobile="123467890"}
Sign up to request clarification or add additional context in comments.

Comments

0

how about

<?php
    echo "<script>";
    echo "var $results = JSON.parse('" . json_encode($results) . "');";
    echo "<script>";
?>

and then use $results in your javascript code anywhere but make sure this script block is loaded before you are using it.

1 Comment

You should not need the JSON.parse() as json_encode() will produce valid javascript object
0

You can try like this to print a php array in javascript

<?php
  $abc=["a","b","c"];
?>
<script type="text/javascript">
  var abc = "<?php echo (implode(',',$abc));?>";
  var abcArr = abc.split(",");
  console.log(abcArr);
</script>

4 Comments

What if there are strings that contain comma in this array? eg. ["a,", "b", "c"]. Additional empty string will be in that array, which is incorrect.
@YuriyYakym, whats your suggestion to prevent this. I just provided the way.
Your solution could be used only in case if we are sure that we use separator which is not a part of any array elements.
Yes totally agreed
0

You can simply use this code:

<?php
    $array = ["a", 1, 2, "b,"];
    echo "<script>";
    echo "var data = ".json_encode($array).';';
    echo "</script>";
?>

The output of this code will be: <script>var data = ["a",1,2,"b,"];</script>, what will create correct javascript array.

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.