Using PHP JSON specific values in JS?

Asked

Viewed 30 times

0

This is my code, but for some reason it does not show the requested data.. I would like to see with 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>
  • There is no need to use formwork for what you are using in the question, please do not reverse the edition.

  • Really, I just realized after hahaha obg

  • I noticed q Voce changed the code, unfortunately my reverse undoes it, Voce can edit and fix your code again?

  • It is not necessary to had turned a variable into string unintentionally

  • Missing are the ; at the end of the lines and this true appears to be invalid.

  • The ; was inattention issue anyway.. are correct in the original code.. I will see about the property true..

Show 1 more comment

1 answer

0

Opa personal, I ended up discovering the error, the script was being loaded before the HTML, hahah beginner error, thanks to the staff who tried to help!

Browser other questions tagged

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