is it possible to run some javascript expression? for example echo eval("Math.sqrt('25')");
You can execute Javascript through PHP by calling javascript code/function as a string in PHP and send it to the client browser to execute.
You need to include the JavaScript file in the markup and then specify a function from it to be executed on click of the input. Show activity on this post. PHP is a server-side language and you can not call JavaScript from it. Because javascript is a client-side language and actually the browser runs it.
php if ($value == 1) { echo '<script type="text/javascript">'; echo '$(document). ready(function(){ /* do_something_in_javascript */ });'; echo '</script>'; } ?>
Answer : PHP and Javascript cannot directly interact since PHP is a server side language and Javascript is a client-side language. However, we can exchange variables since PHP can generate Javascript code to be executed by the browser and it is possible to pass specific variables back to PHP via the URL.
In normal situations :
So, no, it's not quite possible to have PHP execute some Javascript code on the server.
But there is at least on PHP extension that embed (or wrap arround) a Javascript engine, and, as a consequence, allows one to execute Javascript on the server, from PHP.
The extension I'm thinking about is the spidermonkey one : installing and enabling it on your server will allow you to execute Javascript code, on the server, from PHP.
Of course, like any other PHP extension, you'll need to be admin of your server, in order to install it -- and this one is never installed by default, as it answers a very specific need.
About this extension, I have never seen it used in real situations, and there are not many people who tried it... here are two articles you might want to read :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With