可以在 jQuery 脚本中使用 PHP 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6435483/
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-26 00:23:16  来源:igfitidea点击:

Is it okay to use PHP inside a jQuery script?

phpjqueryfunction

提问by hellomello

For example:

例如:

$(document).ready(function(){
    $('.selector').click(function(){
        <?php
        // php code goes here
        ?>
    });
});

Will this cause issues or slow down the page? Is this bad practice? Is there anything important that I should know related to this?

这会导致问题或减慢页面速度吗?这是不好的做法吗?有什么重要的事情我应该知道吗?

Thanks!

谢谢!

回答by Shakti Singh

If you are trying to bound some PHP code with the click event then this is impossible in the way you are trying and PHP code will be executed as soon as page load without waiting for a click event.

如果您尝试将某些 PHP 代码与 click 事件绑定,那么这在您尝试的方式中是不可能的,并且 PHP 代码将在页面加载后立即执行,而无需等待 click 事件。

If you are trying to generate final javascript or jquery code using PHP then this is okay.

如果您尝试使用 PHP 生成最终的 javascript 或 jquery 代码,那么这没问题。

回答by Ross Snyder

It won't slow down the page; the PHP runs on the server and emits text which is sent to the browser, as on any PHP page. Is it bad practice? I wouldn't say "bad" necessarily, but not great. It makes for messy code - in the event where I need to do something like this, I usually try to break it up, as in:

它不会减慢页面速度;PHP 在服务器上运行并发出发送到浏览器的文本,就像在任何 PHP 页面上一样。这是不好的做法吗?我不一定会说“坏”,但也不一定好。它使代码变得凌乱 - 如果我需要做这样的事情,我通常会尝试将其分解,如下所示:

<script>
    var stuff = <?php print $stuff; ?>;
    var blah = "<?php print $blah; ?>";

    // Do things in JS with stuff and blah here, no more PHP mixed in
</script>

回答by Brad Mace

PHP is executed on the server, and then the javascript will be executed on the client. So what you'd be doing here is using php to generate javascript that will become the function body. If that's what you were trying to do then there's nothing wrong with doing it.

PHP在服务器上执行,然后在客户端执行javascript。因此,您在这里要做的是使用 php 生成将成为函数体的 javascript。如果这就是你想要做的,那么这样做并没有错。

If you thought you were going to invoke some PHP code from javascript, then you're on the wrong track. You'd need to put the PHP code in a separate page and use an ajax request to get the result.

如果您认为要从 javascript 调用一些 PHP 代码,那么您就走错了路。您需要将 PHP 代码放在单独的页面中并使用 ajax 请求来获取结果。

回答by Ignacio Vazquez-Abrams

Sure, as long as you keep in mind that PHP code will be executed by the server before the page is sent out. Other than that, have fun.

当然,只要您记住PHP 代码将在页面发送之前由服务器执行。除此之外,玩得开心。

回答by Mauvis Ledford

PHP is a "backend" language and javascript is a "frontend" language. In short, as long as the PHP code is loaded through a web server that understands PHP - the downside is that you have to inline the JS, losing caching ability (there are workarounds to parse php in .js files but you shouldn't really do this). To the user it will just look like javascript and HTML. Here's the server order:

PHP 是一种“后端”语言,而 javascript 是一种“前端”语言。简而言之,只要通过理解 PHP 的 Web 服务器加载 PHP 代码 - 缺点是您必须内联 JS,失去缓存能力(在 .js 文件中有解析 php 的解决方法,但您不应该真的做这个)。对于用户来说,它看起来就像 javascript 和 HTML。这是服务器顺序:

  1. User requests page.
  2. Apache (or equivalent) notices this is a php file. It then renders all the php that are between php tags.
  3. Apache sends the page to the user.
  4. User's browser sees the JavaScript and executes it.
  1. 用户请求页面。
  2. Apache(或等效的)注意到这是一个 php 文件。然后它呈现在 php 标签之间的所有 php。
  3. Apache 将页面发送给用户。
  4. 用户的浏览器看到 JavaScript 并执行它。

Just be sure the PHP is outputting valid JavaScript.

只需确保 PHP 输出有效的 JavaScript。

回答by aymen taarit

you have a better choice to use ajax that runs the php script when you are handling a click event

在处理单击事件时,您可以更好地选择使用运行 php 脚本的 ajax

$(document).ready(function(){
    $('.selector').click(function(){
       $.ajax({url:"phpfile.php",type:"POST",
data:"datastring="+value+"&datastring2="othervalue,

,success:function(data){
//get the result from the php file after it's executed on server
}

});
    });
});

回答by JohnP

No it's not. Just as long as you know that the JS is executed after the PHP page is parsed.

不,这不对。只要你知道 JS 是在 PHP 页面解析后执行的。