Skip to main content
added 340 characters in body
Source Link
IncredibleHat
  • 4.1k
  • 4
  • 18
  • 29

All you need to do is this:

<script> var arr = <?php echo json_encode($query);?>; // <-- no quotes, no parsify console.log(arr); </script> 
  1. All you need to do is this:

    var arr = ; //

JSON = JavaScript Object Notation... its already in the format to assign to a variable to use in javascript.

Although assigning it to a 'const' is ... probably not a good idea since its an object, but its just how I read them, and still perfectly ok: More Info.

  1. If you are calling a php file from js (ala ajax), then you can return raw JSON from the php return, and depending on HOW you made your ajax call (and with what library, like possibly jquery)... you may or may not need to do JSON.parse(returnData). Not enough information provided how you want to do that, to give a solid answer.

All you need to do is this:

<script> var arr = <?php echo json_encode($query);?>; // <-- no quotes, no parsify console.log(arr); </script> 

JSON = JavaScript Object Notation... its already in the format to assign to a variable to use in javascript.

Although assigning it to a 'const' is ... probably not a good idea since its an object, but its just how I read them, and still perfectly ok: More Info.

  1. All you need to do is this:

    var arr = ; //

JSON = JavaScript Object Notation... its already in the format to assign to a variable to use in javascript.

Although assigning it to a 'const' is ... probably not a good idea since its an object, but its just how I read them, and still perfectly ok: More Info.

  1. If you are calling a php file from js (ala ajax), then you can return raw JSON from the php return, and depending on HOW you made your ajax call (and with what library, like possibly jquery)... you may or may not need to do JSON.parse(returnData). Not enough information provided how you want to do that, to give a solid answer.
Source Link
IncredibleHat
  • 4.1k
  • 4
  • 18
  • 29

All you need to do is this:

<script> var arr = <?php echo json_encode($query);?>; // <-- no quotes, no parsify console.log(arr); </script> 

JSON = JavaScript Object Notation... its already in the format to assign to a variable to use in javascript.

Although assigning it to a 'const' is ... probably not a good idea since its an object, but its just how I read them, and still perfectly ok: More Info.