Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing javascript within PHP

Tags:

javascript

php

I believe it is possible to execute a java script function within PHP, but will it then remain server side as opposed to client side? Can I, within PHP, call a .js function, passing it some args, and have it return to the PHP that called it?

A script I want to use returns XML, and I want to get the user inputs using PHP, pass them to the .js function residing on the server, then take the returned xml and parse it back in the PHP part.

I ask because I see people commenting that because .js is client side and PHP is server side, they don't get along. I was hoping that by executing the .js function in the PHP, I could spoof the .js call as coming from the local machine (the server).

Thanks for any information!

like image 398
Robolulz Avatar asked Oct 19 '10 18:10

Robolulz


People also ask

Can JavaScript Connect to PHP?

Yes possible. You can write PHP code in html and can link js also.

How use JavaScript variable in PHP tag?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];


2 Answers

You cannot call JavaScript from within PHP itself. PHP is not a JavaScript engine.

However, there is a PECL Extension for interfacing with V8:

  • http://php.net/manual/en/book.v8js.php
  • http://we-love-php.blogspot.de/2012/07/using-v8-javascript-engine-as-php.html

And you can interface with a (serverside) JavaScript engine. Have a look at node.js and

  • Recommendation for integrating nodejs with php application
like image 69
Gordon Avatar answered Oct 13 '22 00:10

Gordon


You could if you found a server-side Javascript interpreter that you could call out to. I haven't used PHPJS (http://phpjs.berlios.de/) but it might work.

It sounds like your better bet is to replace the js code, because what you're doing just sounds like a bad idea.

like image 39
Mike Ruhlin Avatar answered Oct 13 '22 00:10

Mike Ruhlin