简体   繁体   English

在PHP中执行javascript

[英]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. 这真的不像你看起来那么困难。

  1. Page is rendered initially with PHP/HTML. 页面最初使用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. 使用Jquery Serialize函数将数据转换为可以传递的格式(javascript var)。
  4. Pass serialized data to your Javascript to get the desired XML 将序列化数据传递给您的Javascript以获得所需的XML
  5. Submit Return XML results to PHP via AJAX. 通过AJAX将返回XML结果提交给PHP。
  6. PHP takes XML submitted as POST from Jquery and does its magic. PHP将从Jquery提交的POST作为POST提交并实现其魔力。

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是前端代码与服务器端功能交互的绝佳方式。

停止 PHP 执行<!--? within javascript</div--><div id="text_translate"><p> 我正在尝试实现 TinyMCE,它在我的测试平台上运行良好,但在生产服务器上,PHP 正在尝试执行一些'&lt;?' tiny_mce.js文件中的标签。</p><p> 我有一个名为html_editor.php的文件,它被引入到需要使用include_once的每个表单中。 在html_editor内,我有以下内容:</p><pre> &lt;script type="text/javascript" src="/Public/TinyMCE/tiny_mce.js"&gt;&lt;/script&gt;</pre><p> 随后是 Tiny MCE 初始化,但它在该行上因unexpected T_CONSTANT_ENCAPSED_STRING 。 我用tiny_mce_src.js替换了脚本文件,以找到导致问题的确切代码,它是:</p><pre> html.push('&lt;?', name, ' ', text, '?&gt;');</pre><p> 我在tiny_mce_src.js中用双引号交换了单引号,这确实解决了问题,但在缩小的代码中,它们已经是双引号了。</p><p> 这显然是我的测试服务器和生产服务器之间与 PHP 的配置差异,但我无法追踪是什么。 我正在使用 PHP 5.3 进行测试,并且服务器正在运行 5.2。</p></div> - Stop PHP executing <? within javascript

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 停止 PHP 执行<!--? within javascript</div--><div id="text_translate"><p> 我正在尝试实现 TinyMCE,它在我的测试平台上运行良好,但在生产服务器上,PHP 正在尝试执行一些'&lt;?' tiny_mce.js文件中的标签。</p><p> 我有一个名为html_editor.php的文件,它被引入到需要使用include_once的每个表单中。 在html_editor内,我有以下内容:</p><pre> &lt;script type="text/javascript" src="/Public/TinyMCE/tiny_mce.js"&gt;&lt;/script&gt;</pre><p> 随后是 Tiny MCE 初始化,但它在该行上因unexpected T_CONSTANT_ENCAPSED_STRING 。 我用tiny_mce_src.js替换了脚本文件,以找到导致问题的确切代码,它是:</p><pre> html.push('&lt;?', name, ' ', text, '?&gt;');</pre><p> 我在tiny_mce_src.js中用双引号交换了单引号,这确实解决了问题,但在缩小的代码中,它们已经是双引号了。</p><p> 这显然是我的测试服务器和生产服务器之间与 PHP 的配置差异,但我无法追踪是什么。 我正在使用 PHP 5.3 进行测试,并且服务器正在运行 5.2。</p></div> - Stop PHP executing <? within javascript 从 php 文件中执行 javascript - Executing javascript from within a php file 单击按钮时在 javascript 中执行 php 代码 - executing php code within javascript on button click PHP中生成的Javascript未执行 - Javascript generated in PHP is not executing 在JavaScript中执行PHP脚本? - Executing a php script in javascript? Javascript未用PHP执行 - Javascript not executing with PHP JavaScript执行PHP代码 - javascript executing php code PHP 生成 javascript 未执行 - PHP generated javascript not executing 从php中执行python - Executing python from within php 在PHP中执行shell脚本 - Executing shell script within PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM