Is it possible to enable and call a JavaScript function with a PHP script? And How?

I don’t know how I am going to do this, but ...

I have a server-side, PHP CLI script. It will be launched from the command line. My PHP script should call a function in the JavaScript library and get the return value. I need to find a way to start a JavaScript server. How can I achieve this?

Decision:

Using Rhino , you can run JavaScript as a command line script . First download and extract Rhino . Then, assuming your scripts look like this:

PHP:

<?php # myscript.php $pathToRhinoJar = './rhino1_7R2/js.jar'; $javascriptFile = './test.js'; $output = shell_exec("java -jar $pathToRhinoJar $javascriptFile command line arguments"); echo "Response from javascript:\n $output"; 

JavaScript:

 /* test.js */ for (i in arguments) { print(arguments[i]) } 

This will be the result:

 $ php ./myscript.php Response from javascript: command line arguments 
+4
source share
7 answers

Well, technically you could do what you want.

  • Find a command line JS interpreter, this may be a good starting point
  • Make a JS script that accepts input and produces output
  • Start doing everything you do in php
  • Throw away $output = system('./jsInterpreter -param value', $retval); or similar
  • Do something with the data

EDIT: Rhino seems to be perfect for your needs:

Predefined Properties

Scripts executed in the shell have access to some additional properties of the top-level object. Arguments

The arguments object is an array containing the strings of all arguments specified on the command line when the shell is called.

+2
source

If you are not talking about any form on the client side or on the server side of JavaScript, then what you are talking about will not work.

PHP runs on the server side.

JavaScript runs on the client side.

You cannot “invoke a method” on another, because PHP is already executed before the client can invoke JavaScript.

The only way to do something like this work is to provide some way to execute JavaScript, and then send the result of the method back to the server using an AJAX call.

+3
source

I think this is what you are looking for http://devzone.zend.com/article/4704 . Using JavaScript in PHP with PECL and SpiderMonkey

This example is not even close to tested, but I think that it should lead you in the right direction.

 -- funcs.js function add(a,b) { return a +b; } -- use-js-funcs.php <?php // create JavaScript context $js = new JSContext(); $js_code = file_get_contents('func.js'); // define Script code $js_code.= "\n add(5)"; // evaluate script and display result, // my guess is that evaluateScript returns the // result of the last statement that was executed echo $js->evaluateScript($script); ?> 
+1
source

PHP can only perform the functions of the preliminary process, so I believe that the only way to call the JavaScript function on the page is to print a JavaScript call:

 ?> <script> functionToCall(); </script> <?PHP 

In this ToCall function, you can use the AJAX function or send a form to send data back to your PHP code for processing.

0
source

PHP runs on your server and the output is sent to the browser. The browser executes the script as provided by PHP. No, PHP cannot call a JavaScript function - it can only serve a browser page that contains JavaScript that the browser will execute.

This script may contain an AJAX callback that tells the browser to initiate a new request to the PHP script server - but this is a separate and separate request to the server.

0
source

Do I need to run the script on the client side? You can write a function that starts and calls the JS library right after the page loads, and then sends the return value to your script with an XHR call. You can also set a timeout to run the function and do it after a certain period of time.

0
source

If you want this to work, you will have to initiate a call through JavaScript using Ajax.

That's what I'm doing:

First, create a PHP file that does all the work you need, and then ask it to perform a simple set of results. Sort of:

 echo 'true'; 

The key should be simple. If you need more than just outlining each result with a space, pipe, or coma, you can subsequently split it into an array.

Secondly, you need to make a javascript call with ajax. That's what I'm doing:

 var result = null; var scriptUrl = "your-php-page.php"; $.ajax({ url: scriptUrl, type: 'get', dataType: 'html', async: false, success: function(data) { result = data; } }); return result; 

Then you can do whatever you need with the returned data. You can even send it to another PHP page if you need to.

0
source

All Articles