r/PHPhelp • • Mar 18 '26
I need to take a value from database as PHP variable and somehow convert that variable to be compatible with a Javascript
I have a situation where I need a button created with Javascript if conditions set in the database table say so. I basically have it figured out how to get the value and stuff, but using an if equals value with php doesn't allow me to run the Javascript within. What's the best way to pass this information/value into a variable that's usable with the Javascript? Thanks.
8
Upvotes
5
u/BaronOfTheVoid Mar 19 '26 edited Mar 19 '26
I think the recommended way of doing this should be:
This way you still have full separation of JS and PHP in terms of the file structure and are safe against injection. Also the JS global namespace isn't polluted with random data. It also works with any data type, even objects.
I think trying to generate JS output with PHP is an architectural flaw in itself. Especially should you at some point make the decision to, idk, move to Typescript and some build process or something like that.
2
u/colshrapnel Mar 19 '26
Surely it does?
But well, to answer your request for passing a PHP value into JS. it's really simple too, using a
<script>tag and json_encode(). Only you must remember that any input into HTML must be html-encoded too.After landing in the browser, this JS code will be executed and you will have the javascriptVariable at your disposal.
8
u/DevelopmentScary3844 Mar 18 '26
PHP -- write data -> HTML data attribute <- read data attribute -- JS
1
u/[deleted] Mar 19 '26
I wanted to let everyone know 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!
Thank you all for your input, it was much appreciated, in the end, I was able to find a way with the right idea!
1
u/MateusAzevedo Mar 19 '26
Is "with Javascript" really needed here? Can't PHP do that already?
Something like
3
u/Aggressive_Ad_5454 Mar 18 '26
The accepted way to do this is to json_encode the data, and write out the string to your html in a <script> tag.
5
u/martinbean Mar 18 '26
Well it completely depends what this “value” is.
It would help us to help you if you gave us some context about the actual problem you’re trying to solve instead of how you’re trying to solve it.
2
u/birdspider Mar 18 '26
you are aware that those two run on different computers? (server -> client/browser)
There is no "within" or "covert", just:
DB <=> php <=> http/request/response <=> browser/html+js <=> you-clicking-button0
u/AmiAmigo Mar 19 '26
Hope this helps:
https://chatgpt.com/share/69bb50db-4a20-800e-9296-7ad7155eea95
1
u/latro666 Mar 18 '26
I'm not a front end person but in the past i'd add an attribute to an element the button would be in like <div class="showButton" data-button="show">
But like php driven so the show bit woukd be <?= $showButton ?> which is defined by the if. E.g .
If(database = whatever){ $showButton = "show"; }
Then the js has an event on document load or whatever that looks for that data att and appends the button if set.
Very old school probably old outdate way of doing it but would work
1
u/AshleyJSheridan Mar 19 '26
1
u/ThePalsyP Mar 19 '26
If you do use a data- tag, make sure you sanitise it in the backend....... HTML can be manipulated.
1
u/Big-Dragonfly-3700 Mar 21 '26
Why are you using javascript to create a button based on a value from the server-side code? Simply, conditionally output the markup for the button from the server-side code.
1
u/99thLuftballon Mar 18 '26
The traditional way is to use PHP to write an html script tag defining the variable above where your Javascript is imported.
E.g.
echo <<<HTML
<script> const javascriptVariable = $phpVariable </script>
HTML;
1
u/[deleted] Mar 19 '26
[removed] — view removed comment