在 .js 文件中执行 PHP 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23574306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:48:38  来源:igfitidea点击:

Executing PHP code inside a .js file

phpjquerywordpress

提问by Keryn Gill

I'm trying to get a bit of PHP to execute inside of a .js file, but obviously don't know how to do it properly.

我正在尝试在 .js 文件中执行一些 PHP,但显然不知道如何正确执行。

Basically the code is adding some HTML tags to my page, which I am using for a slideout contact form. However the contact form itself is done in Wordpress by a shortcode. So I'm trying to get the shortcode to work inside the code that makes the form slide out.

基本上,代码是向我的页面添加一些 HTML 标签,我将其用于滑出式联系表单。然而,联系表格本身是通过短代码在 Wordpress 中完成的。所以我试图让短代码在使表单滑出的代码中工作。

var phpTest = '<?php do_shortcode("[contact-form-7 id=\'1088\' title=\'Live Chat Form\']"); ?>';

// add feedback box
$this.html('<div id="fpi_feedback"><div id="fpi_title" class="rotate"><h2>'
    + thisSettings.title
    + '</h2></div><div id="fpi_content">'
    + phpTest
    + '</div></div>');

If I change the variable "phpTest" to a regular string with text, it shows up fine, I'm just not sure how to execute the php in this instance.

如果我将变量“phpTest”更改为带有文本的常规字符串,它显示得很好,我只是不确定如何在这种情况下执行 php。

回答by chiliNUT

EDIT: The script type should always be text/javascriptas application/javascriptmay break compatibility with older browsers (I think IE8 and earlier will ignore the script tag with that type). The Content-type, however, should still be application/javascript.

编辑:脚本类型应始终text/javascriptapplication/javascript可能会破坏与旧浏览器的兼容性(我认为 IE8 及更早版本将忽略具有该类型的脚本标记)。但是,内容类型仍应为application/javascript.



You can't mix in php into a .jsfile, the way you would with a normal .phpfile, since the .jsfile is not preprocessed by the .phpengine the way your .phpfiles are.

您不能像.js处理普通.php文件那样将php 混合到一个文件中,因为该.js文件没有.php像您的.php文件那样由引擎进行预处理。

The most typical way to do this--including php in a js file--is to just have inline JS (stuff between <script>tags) in your .php file. Then it will work as expected.

执行此操作的最典型方法(包括在 js 文件中使用 php <script>)是在 .php 文件中只包含内联 JS(标记之间的内容)。然后它将按预期工作。

Another way to go is to make a js file within php, so start your .php file with

另一种方法是在 php 中创建一个 js 文件,因此在 .php 文件中使用

<?php
header("Content-type: application/javascript");
?>
var jsvar='something';
var othervar='<?php echo $phpVar; ?>';
//<?php //some php ;?>

and then include it on your page like this

然后像这样将其包含在您的页面上

<script src='myjs.php' type='text/javascript'></script>

回答by Brian Graham

I commented saying your question is not possible, however it is indeed possible. This is not recommended, but if you're not worried about the possible security holes, here is a way to accomplish your question:

我评论说你的问题是不可能的,但它确实是可能的。不推荐这样做,但如果您不担心可能存在的安全漏洞,这里有一种方法可以解决您的问题:

Create a .htaccessfile in the same directory as your .jsfiles, with the following:

.htaccess在与您的.js文件相同的目录中创建一个文件,内容如下:

AddHandler application/x-httpd-php .js

<FilesMatch "\.js$">
    Header set Content-Type "text/javascript; charset=utf-8"
</FilesMatch>

This will execute your JavaScript files as PHP, then fix the Content-Type for .js files automatically (because they're seen as text when you add the php handler).

这会将您的 JavaScript 文件作为 PHP 执行,然后自动修复 .js 文件的 Content-Type(因为在您添加 php 处理程序时它们被视为文本)。

Note: While this is a quick and dirty solution, a better way would be to use Ajax (see chiliNUT's Answer)

注意:虽然这是一个快速而肮脏的解决方案,但更好的方法是使用 Ajax(请参阅chiliNUT 的答案

回答by Jayo2k

You can do a ajax post request to a .php file. It is a little dirty trick but it works

您可以对 .php 文件执行 ajax post 请求。这是一个小技巧,但它有效

$.post('thePhpFile',{var1:var1,var2:var2},function(result){
    //Code to do with the resulted... like $("#div").html(result);
});

With that trick, you can basicaly do anything you want

有了这个技巧,你基本上可以做任何你想做的事

回答by Jo?o Paulo de Lima

I think you can do something like this using AJAX:

我认为你可以使用 AJAX 做这样的事情:

JS code:

JS代码:

var xmlhttp;
if (window.XMLHttpRequest){
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}else{
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        $this.html('<div id="fpi_feedback"><div id="fpi_title" class="rotate"><h2>'
        + thisSettings.title
        + '</h2></div><div id="fpi_content">'
        + xmlhttp.responseText
        + '</div></div>');
    }
}

xmlhttp.open("GET","do_shortcode_file.php",true);
xmlhttp.send();

The do_shortcode_file.php must have something like this:

do_shortcode_file.php 必须是这样的:

PHP code:

PHP代码:

<?php
echo do_shortcode("[contact-form-7 id=\'1088\' title=\'Live Chat Form\']");

回答by Hidran

Suppose your filename is myscript.js:

假设您的文件名是 myscript.js:

in your head tag ( it must be an executable php file):

在您的 head 标签中(它必须是一个可执行的 php 文件):

<head>
<script type="text/javascript">
<?php 
require_once 'myscript.js';
?>
</script>

... ... in this way you are able to execute that file as if it were a php file

... ... 这样您就可以像执行 php 文件一样执行该文件