Using specific values of a PHP JSON in JS?

0

This is my code, but for some reason it does not show the requested data .. I would like to see you if I am doing something wrong.

PHP

<?php
    $var1 = 1;
    $var2 = 2;
    $var3 = 3;

    $myobj -> foo1 = $var1;
    $myobj -> foo2 = $var2;
    $myobj -> foo3 = $var3;

    $json = json_encode( $myobj, true );
?>

javascript:

<script>
    var json = <?php echo "$json"; ?>;

    document.getElementById("ID").innerHTML = json.foo2;
</script>
    
asked by anonymous 05.06.2018 / 01:21

1 answer

0

Oops, I discovered the error, the script was being loaded before the HTML, hahah beginner error, thank you to the people who tried to help!

    
05.06.2018 / 05:50