Execute a javascript function from PHP and return the response

I am trying to execute a javascript function from a PHP script by sending a variable over to my javascript function and then it will return the response. I want the PHP script to only echo out the response from the javascript function, so when a client views the source code of my php file, he ONLY sees the response from the javascript function and not the javascript source code.

for example,

I have a javascript function called validate(), and a PHP file called check.php. The client calls the function using the GET method like so,


I then want check.php to send the variable $value over to my javascript function validate(), to perform the following,


then the javascript to return the value response to my PHP script and store it within a variable $response, and then it will echo $response. So when I view the source, I will only see the contents within the variable answer.

I can't find a simple way to do this :S. If I use AJAX I will need to use a div within HTML, but it's important that ONLY the contents of $response is displayed within the source code?

Could someone point me into what PHP functions I should be using ? Thank you!


PHP normally runs server-side, and Javascript normally runs client side (in the browser).

In the normal model, the client makes requests to the server, and the server responds. In this case it is not possible for the PHP code to "invoke" the Javascript function directly.

So what is the solution to your problem?

In order of preference:

  1. The first, best option is to re-architect so that you do not need to call the javascript validate() function from within PHP. For example, port the Javascript logic to PHP.

  2. If you cannot port, run Javascript on the server side. Node.js runs on server-side on Linux and Windows; if you have Windows/IIS, the easier option is to serve Javascript via ASP classic, which, yes, is still available on current Windows Servers. The PHP program can simply request a local Javascript resource probably via curl; the server invokes the logic and returns the result. In the general case the Javascript need not run on the same server.

  3. Finally, if that's not an option, you could have the browser execute the validate() on behalf of the server. The PHP can emit the validation function, in a javascript script block, to the browser. PHP can also emit the thing that needs to be validated - a data packet, or whatever it is. The browser can then validate() on that object, and post a message to PHP via AJAX to report the result. But, there's a security issue here - keep in mind that the client is outside of your control, so there's a possibility that someone will subvert the client-side validation logic and cause the client to report false results to the PHP server. You cannot count on tihis.

Therefore you'd need to embed some sort of data integrity check in the transaction - some way that you could insure that the client-side javascript has not been modified. This will be hard.

You shouldn't be suing any functions :P

However, while it is a seemingly horrible way to do a validation, you could do the following:

  • Have the javascript output to the HTML file. (Put it somewhere where it will be easy to find)
  • Use fopen to open a URL with your parameters in the URL that will be picked up by the js function.
  • Have your PHP function parse the HTML file (that you used fopen to get) for output and search for the specific output you want.

Really though, you should just do the validation in the PHP script itself. Much safer, much easier and much easier to work with later if you want to change something.

Need Your Help

Check for multidimensional list in Python

python list multidimensional-array

I have some data which is either 1 or 2 dimensional. I want to iterate through every pattern in the data set and perform foo() on it. If the data is 1D then add this value to a list, if it's 2D the...

Size of 802.11 packet

c pcap libpcap wlan

Given the below declaration for pcap callback: