nodejs execute php script code example
Example 1: php execute javascript
<?php
if(your condition){
echo "<script> window.onload = function() {
yourJavascriptFunction(param1, param2);
}; </script>";
?>
Example 2: run php on node js server
var http = require("http"),
fs = require("fs"),
path = require("path"),
url = require("url"),
runner = require("child_process");
function sendError(errCode, errString, response)
{
response.writeHead(errCode, {"Content-Type": "text/plain;charset=utf-8"});
response.write(errString + "\n");
response.end();
return false;
}
function sendData(err, stdout, stderr, response)
{
if (err) return sendError(500, stderr, response);
response.writeHead(200,{"Content-Type": "text/plain;charset=utf-8"});
response.write(stdout);
response.end();
}
function runScript(exists, file, param, response)
{
if(!exists) return sendError(404, 'File not found', response);
runner.exec("php " + file + " " + param,
function(err, stdout, stderr) { sendData(err, stdout, stderr, response); });
}
function php(request, response)
{
var urlpath = url.parse(request.url).pathname;
var param = url.parse(request.url).query;
var localpath = path.join(process.cwd(), urlpath);
fs.exists(localpath, function(result) { runScript(result, localpath, param, response)});
}
var server = http.createServer(php);
server.listen(1000);
console.log("PHP ready to run script given on port 1000.");
Example 3: how to run php inside js
You can't run PHP with javascript. JavaScript is a client
side technology (runs in the users browser) and PHP is a
server side technology (run on the server). If you want to
do this you have to make an ajax request to a PHP script and
have that return the results you are looking for.