tags:

views:

121

answers:

4

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!

+10  A: 

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

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

Gordon
Looks interesting. I'm going to give it a look. Thanks.
Robolulz
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
@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
@Gordon 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
@Robolulz the big questionmark is whether you really need PHP in the middle.
Gordon
+2  A: 

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
A: 

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
+1  A: 

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. 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); //you might want to include this file seperately
$('#invisibleDiv').html('<form id="yourForm" method="POST"><input type="hidden" name="x" val="'+yourXML+'" /></form>'); $('#yourForm').submit();
}

jon_darkstar