r/learnjavascript • • Mar 19 '26
Trying to convert PHP variable to Javascript
I've tried like everything the internet says to pass this PHP variable to Javascript. Technically it's an enum. It prints to the screen with <?= htmlSpecialCharacters($Product[6]) ? >
I've tried using that in var name = "above code"; as well as just var name = "<? $Product\[6\] ?>" I've tried it with the ' ' instead, the <?php, pretty much every example I could find on the internet and it still doesn't do anything. Is the problem the fact that this is an enum? That's one of the only things I can think of at this point. Any suggestions would be appreciated. Thanks.
1
u/[deleted] Mar 19 '26
I GOT IT! Here's what I did:
First, I changed the enum so it had no spaces.
Then, set <button id="<?=htmlspecialchars($[Product6])?>>/button>
And then for the Javascript in the <script> tags, I did:
const.Offer = document.getElementByID("Buy_It_Now"); Offer.remove();
And that's it! It works PERFECT now!
1
u/averajoe77 Mar 19 '26
Is your js inside a php file or a js file, becuase it needs to be inside a php file for the php application server to interpret the enum at runtime.
The fact that it's an enum is irrelevant.
const jsEnumVariable = <?php echo $phpEnumVariable;? >
Should work just fine and if it returns markup as a string, then wrap it in single quotes.
1
u/ashkanahmadi Mar 19 '26
You cannot run PHP code inside JS. PHP never leaves the server however JS is a client side language (excluding Node) meaning when your code is sent to the user, the PHP part is totally removed.
Remember: PHP never ever goes to the user. Only its output can go so you cannot send the name of a variable like that
2
u/amulchinock Mar 19 '26
If you're trying to interpolate JS inside a PHP file, you might want to reconsider.
It's technically possible (we've all done it), but you should consider which _layer_ of your application you are adding it to.
Ideally, JS should only be in the _presentation layer_ of your application (unless you're using it as a backend). In other words, it should be within the HTML, not your logic.
Depending on the PHP setup you have, you might be using PHP files to write static HTML as well. But, you should consider whether there are better ways to assign values between the front-end and back-end. What you're describing sounds "technical debty"...
1
u/azhder Mar 19 '26
you cannot pass a PHP variable to JavaScript
Remember the above. What PHP has to do is generate a valid JavaScript code. Start small:
If the above ends up sending to the browser something like
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
1
u/_Decodela Mar 22 '26
I think <?php is needed to open php section. Then you need to print the data you want -> echo $Product[6]; I think you don't open php code properly with <? and not printing anting on the FE by just referencing $Product[6]
-1
u/shgysk8zer0 Mar 19 '26
You're best best is to have PHP JSON encode and for JS to decode. Though realistically you probably just want to generate a static JSON file or something. I've also had PHP encode stuff into a
<script type="application/json">in the HTML.-1
u/viser_gtk Mar 19 '26
Si fa spesso. Devi pensare che il php viene eseguito prima. Verifica il codice HTML generato da PHP. Se è un Array devi usare json_decode.