[英]Executing javascript within PHP
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? 我相信可以在PHP中执行一个java脚本函数,但是它会保留在服务器端而不是客户端吗? Can I, within PHP, call a .js function, passing it some args, and have it return to the PHP that called it? 我可以在PHP中调用.js函数,传递一些args,并让它返回到调用它的PHP吗?
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. 我想要使用的脚本返回XML,我希望使用PHP获取用户输入,将它们传递给驻留在服务器上的 .js函数,然后获取返回的xml并在PHP部分中将其解析回来。
I ask because I see people commenting that because .js is client side and PHP is server side, they don't get along. 我问,因为我看到人们评论,因为.js是客户端而PHP是服务器端,他们不相处。 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). 我希望通过在PHP中执行.js函数,我可以将.js调用欺骗来自本地机器(服务器)。
Thanks for any information! 感谢您的任何信息!
You cannot call JavaScript from within PHP itself. 你不能从PHP本身调用JavaScript。 PHP is not a JavaScript engine. PHP不是JavaScript引擎。
However, there is a PECL Extension for interfacing with V8: 但是,有一个PECL扩展用于与V8连接:
And you can interface with a (serverside) JavaScript engine. 您可以与(服务器端)JavaScript引擎连接。 Have a look at node.js and 看看node.js和
You could if you found a server-side Javascript interpreter that you could call out to. 如果您找到了可以调用的服务器端Javascript解释器,您可以。 I haven't used PHPJS (http://phpjs.berlios.de/) but it might work. 我没有使用过PHPJS(http://phpjs.berlios.de/),但它可能有效。
It sounds like your better bet is to replace the js code, because what you're doing just sounds like a bad idea. 听起来你更好的选择是替换js代码,因为你所做的只是听起来不错。
I think bpeterson nailed it. 我认为bpeterson钉了它。 But if you are uncomfortable with AJAX, or just need a little more specifics. 但是如果你对AJAX感到不舒服,或者只是需要更多细节。
First - Put an action on a button (form submit or otherwise). 首先 - 对按钮(表单提交或其他方式)执行操作。 action="javascript:yourJsFunc()". 行动= “JavaScript的:yourJsFunc()”。 This element is likely being rendered thru echos on your PHP, or just written statically. 这个元素很可能通过PHP上的回声呈现,或者只是静态编写。
Next - Get the parameters you need. 下一步 - 获取所需的参数。 I'd suggest jQuery or DOM methods, ie. 我建议使用jQuery或DOM方法,即。 $('#blah') or document.getElementById('blah').val(); $('#blah')或document.getElementById('blah')。val();
Then - Set hidden inputs to store your response, force a submit or do ajax request. 然后 - 设置隐藏的输入以存储您的响应,强制提交或执行ajax请求。 You are in PHP with your values! 你有PHP的价值观!
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});
}
This is really not as difficult as you're making it seem. 这真的不像你看起来那么困难。
FYI, there's a relatively little-known solution for AJAX via PHP called XAJAX . 仅供参考,通过PHP称为XAJAX的 AJAX解决方案相对鲜为人知。 It probably wouldn't help you out too much in this situation, but it allows you to make AJAX calls on PHP functions. 在这种情况下,它可能不会帮助你太多,但它允许你在PHP函数上进行AJAX调用。 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. 虽然PHP没有javascript解释器,但XAJAX和Jquery的Ajax是前端代码与服务器端功能交互的绝佳方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.