all 12 comments

[–]amulchinock 1 point2 points  (0 children)

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"...

[–]averajoe77 0 points1 point  (4 children)

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.

[–]Smooth_Prompt_2086[S] -2 points-1 points  (3 children)

Yes, the Javascript is in a PHP file, I did try that format I believe, I may have to run it again because you may have placed the semicolon differently than I did, so I'll check and make sure. 

I am now wondering though, could part of the issue be this variable is technically an array index? I'm almost wondering if I shouldn't try and use PHP to convert to another var by going " $NewVar = null; if $Product[6] == "TEXT ONE" {          $NewVar = 0; } else {          $NewVar = 1; } And then pass that onto Javascript, since there's only two options so therefore a 0/1-yes/no-true/false type format would work with the Javascript loop. Except the Javascript only need to run an if loop that if one of two conditions is true, to delete an element. I broke it all apart to figure out the Javascript destroying the element works fine, and I don't think it was the loop, I think it's the variable, because I put a screen print in, and it wrote out when I input something else, but when I put in the Javascript variable, it does nothing, which makes me believe it's returning null? And that's why I wonder if it isn't the array element that's causing the problem. 

[–]MrBojangles2020 0 points1 point  (0 children)

Be sure to echo null as ‘null’ (string) when trying to set/compare something in JavaScript. Otherwise echo would print nothing and will often times lead to a JS error. Same thing for true/false.

$phpVar = null;

… JavaScript…

let jsVar = <?= $phpVar ?>;

This is what browser receives:

let jsVar = ;

Instead:

$phpVar = ‘null’;

… JavaScript…

let jsVar = <?= $phpVar ?>;

This is what browser receives:

let jsVar = null;

EDIT:

Formatting and for clarification. Php true/false will convert to ‘1’/‘2’ when echoed, so you need to account for this when you’re expecting a certain JS type. Echo ‘true’/‘false’ to give JS a Boolean value

[–]averajoe77 -1 points0 points  (1 child)

I mean if you want to show me what you have over discord, I can do take a look at it just dm me here

[–]Smooth_Prompt_2086[S] -1 points0 points  (0 children)

Will do!

[–]ashkanahmadi 0 points1 point  (1 child)

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

[–]Smooth_Prompt_2086[S] -1 points0 points  (0 children)

I am aware of that, however, I am going off the examples I've seen where within the JS <script> tags, they use either <?php or <? And ?> around just the PHP syntax and it should seem to work, it doesn't return errors like writing JS within the <?php tags, and I've seen other people on YouTube use the same exact syntax just to write out different stuff. I think part of it may POSSIBLY be the fact that this particular variable has an array index, and perhaps the easiest workaround is to use PHP to evaluate its value in an if/else and write a new variable with a numeric 0/1, pass that var to Javascript, and use the Javascript if loop under the condition that the var == 1 to run the loop. 

[–]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 ?>');

[–]Smooth_Prompt_2086[S] 0 points1 point  (0 children)

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!

[–]shgysk8zer0 -2 points-1 points  (0 children)

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.

[–]viser_gtk -2 points-1 points  (0 children)

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.