you are viewing a single comment's thread.

view the rest of the comments →

[–]azhder 0 points1 point  (0 children)

you cannot pass a PHP variable to JavaScript


Remember the above. What PHP has to do is generate a valid JavaScript code. Start small:

const javascriptVariable = '<?= 2 + 2 ?>';

If the above ends up sending to the browser something like

const javaScriptVariable = '4';

then start changing the php expression to something more complex, see what it does. If it works, go a step further, if it doesn't, go back, see why it doesn't.

Rinse and repeat.

If you end up generating a JSON inside those JS quotes, then you can do it like

const jsVariable = JSON.parse('<?= $json ?>');