Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I've been there.
echo json_encode($variable); inside a script tag, after a variable definition, in place of the value. That if it's the same script.
AJAX if it's another online script, look it up.
Remember JS works after PHP sends the whole markup, so it's not synchronous.
Good luck! -
nitnip18083yPHP to JS:
let var = '<?= json_encode($var) ?>';
JS to PHP
Ajax, XMLHttpRequest, fetch. -
sariel85343yIt's called AJAX.
https://w3schools.com/xml/...
The true test of a dev is to develop native Ajax calls without the use of a JavaScript framework. 😂 -
It's easy the other way around, but for this, AJAX is the only solution, or normal form submission and set value by javascript.
-
hitko31483y@sariel The true test is not to use obsolete shit 7 years after all major browsers introduced fetch.
const data = await fetch('http://example.com/movies.json')
.then(response => response.json()) -
sariel85343y
Is there a way to pass JavaScript variables to PHP?
question