5

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!

Robolulz
  • 178
  • 1
  • 7
  • The short answer is no. I think you might need to change the design of your application a little bit. Could you implement the function that returns XML in PHP (or is there a PHP equivalent that accomplishes what the JS does)? – Cᴏʀʏ Oct 19 '10 at 18:04
  • PHP does not have a Javascript interpreter. [Server side Javascript](http://en.wikipedia.org/wiki/Server-side_JavaScript) does exist, but wouldn't it be easier just to reimplement the JS function in your PHP code? – Daniel Vandersluis Oct 19 '10 at 18:07
  • The Javascript is third party. I wish there was a PHP version. – Robolulz Oct 19 '10 at 18:11

4 Answers4

13

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

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

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

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Looks interesting. I'm going to give it a look. Thanks. – Robolulz Oct 19 '10 at 18:30
  • What I have is a file search application running for my storage server and it has a Javascript interface. The web front-end for the server is currently PHP, as it should be. I just wanted to include the search capability into the web front-end. – Robolulz Oct 19 '10 at 18:33
  • @Robolulz by web-front end you mean the part that is run in the browser? If so, why dont you call the storage server JS interface directly from the browser instead of from PHP? Also, is the storage server, by any chance, a CouchDB or other NoSQL DB? – Gordon Oct 19 '10 at 18:50
  • Sorry, hopefully this helps. I've got a linux machine running as my fileserver and I've given it a web frontend to handle some of the content management users wanted. That's all in PHP; simple enough. I just want to add the ability to search the files on the system on the web front-end. The in-file search I'm using currently only provides a Javascript interface for performing the search, and it returns xml with the search results (file location, ranking, etc). I need to get the user input from the PHP to the Javascript, then the XML results from the Javascript to the PHP. – Robolulz Oct 19 '10 at 19:04
  • @Robolulz the big questionmark is whether you really need PHP in the middle. – Gordon Oct 19 '10 at 19:15
3

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.

Mike Ruhlin
  • 3,546
  • 2
  • 21
  • 31
1

I think bpeterson nailed it. But if you are uncomfortable with AJAX, or just need a little more specifics.

First - Put an action on a button (form submit or otherwise). action="javascript:yourJsFunc()". This element is likely being rendered thru echos on your PHP, or just written statically.

Next - Get the parameters you need. I'd suggest jQuery or DOM methods, ie. $('#blah') or document.getElementById('blah').val();

Then - Set hidden inputs to store your response, force a submit or do ajax request. You are in PHP with your values!

function yourJsFunc()  
{  
   var arg1 = $('#arg1').val //or equivalent DOM method  
   var arg2...  
   //serialize these if necessary  
   var yourXML = outsideJSFunction(arg1, arg2, etc);

   $('#invisibleDiv').html('<form id="yourForm" method="POST"><input type="hidden" name="x" val="'+yourXML+'" /></form>'); 
   $('#yourForm').submit(); 
        //ALTERNATIVELY $.get("other.php", {myXml : yourXml}, function() {//whatever}); 
}  
jon_darkstar
  • 16,398
  • 7
  • 29
  • 37
0

This is really not as difficult as you're making it seem.

  1. Page is rendered initially with PHP/HTML.
  2. User enters information into form fields.
  3. Use the Jquery Serialize function to get the data into a format (javascript var) you can pass.
  4. Pass serialized data to your Javascript to get the desired XML
  5. Submit Return XML results to PHP via AJAX.
  6. PHP takes XML submitted as POST from Jquery and does its magic.

FYI, there's a relatively little-known solution for AJAX via PHP called XAJAX. It probably wouldn't help you out too much in this situation, but it allows you to make AJAX calls on PHP functions. While it's true that PHP doesn't have a javascript interpreter, XAJAX and Jquery's Ajax are excellent ways for the front end code to interact with the server-side functionality.

bpeterson76
  • 12,918
  • 5
  • 49
  • 82