PHP 函数中的 JavaScript jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23585364/
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
JavaScript jQuery inside PHP function
提问by HackerManiac
This is a theoretical question. My question is whether a jQuery function or script can be written inside a PHP function. E.g.
这是一个理论问题。我的问题是是否可以在 PHP 函数中编写 jQuery 函数或脚本。例如
<?php function phpfunc(){
$a=10; ?>
<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>
Is this possible and legal?
这是可能的和合法的吗?
采纳答案by Niet the Dark Absol
Yes, it's okay to do that. No, it's probably not a good idea. But there's nothing really stopping you.
是的,这样做没问题。不,这可能不是一个好主意。但没有什么能真正阻止你。
Just be aware that if your variable happens to have a '
in it, you'll get messed up.
请注意,如果您的变量中恰好有 a '
,您会搞砸的。
So, whenever you want to pass a variable from PHP to JavaScript, be sure to use json_encode
:
因此,每当您想将变量从 PHP 传递到 JavaScript 时,请务必使用json_encode
:
<script type="text/javascript">
alert(<?php echo json_encode($something); ?>);
</script>
Note that there's no quotes in the JavaScript part - json_encode
will add quotes if needed. This can be used to pass almost any kind of variable.
请注意,JavaScript 部分中没有引号 -json_encode
如果需要,将添加引号。这可用于传递几乎任何类型的变量。
回答by Anoop Sharma
Yes. It is possible and Legal one too. we generally use the same when we require any server side value to be set on client-side on runtime.
是的。这是可能的,也是合法的。当我们需要在运行时在客户端设置任何服务器端值时,我们通常使用相同的方法。
Hope this answers your query.
希望这能回答您的疑问。
Thanks Much!
非常感谢!
回答by Bart?omiej Wach
When php code is interpreted by the sever writing something like:
当服务器解释 php 代码时,编写如下内容:
<?php
function foo()
{
<script type=text/javascript> ... </script>
}
?>
As part of the code in <?php ?>
is interpreted as php and string inside the function doesnt represent any of php functions
由于代码中的一部分<?php ?>
被解释为 php 并且函数内的字符串不代表任何 php 函数
You can echo javascript code (or any content of a HTML document) through your php code like:
您可以通过 php 代码回显 javascript 代码(或 HTML 文档的任何内容),例如:
<?php
function foo(){
echo "<script type=text/javascript> alert('it works!)'; </script>";
} ?>
so when you execute the function, you wil add the javascript to the document by echoing it and therefore execute it.
因此,当您执行该函数时,您将通过回显将 javascript 添加到文档中,从而执行它。
You can also use php variables to echo variables to javascript like:
您还可以使用 php 变量将变量回显到 javascript,例如:
<?php
function foo(){
echo "<script type=text/javascript> alert('{$phpVariable}'); </script>";
} ?>
or
或者
<?php
function foo(){
echo "<script type=text/javascript> var variableFromPHP = {$phpVariable}; </script>";
} ?>
回答by Patrick
The short answer is no(edit : yes).
简短的回答是否定的(编辑:是)。
javascript is executed on the client and only on the client.
javascript 在客户端上执行,并且仅在客户端上执行。
You can however echo javascript to the client.
但是,您可以向客户端回显 javascript。
So something like this :
所以像这样:
$JSfunction = "<script>alert('This is working')</script>";
can be echoed to the page by doing echo $JSfunction;
可以通过执行回显到页面 echo $JSfunction;
edit :
编辑 :
Since you didn't mention where that function is located, I assumed you meant the PHP function on the server side.
由于您没有提到该函数的位置,我假设您指的是服务器端的 PHP 函数。
To be clear, If it's written on the html page itself, it's perfectly fine and can be done.
要清楚,如果它是写在 html 页面本身上,那完全没问题,可以完成。
complete answer
完整的答案
<? function phpfunc(){
$a=10; ?>
<script>var a ='<?php echo $a ?>'</SCRIPT> <?php } ?>
<?php phpfunc() ?>
<script>console.log(a);</script>
You must echo that $a
你必须回应 $a
回答by Shashank Shekhar
Yes you may use it like in normal html page. Use script include inside echo inside the php script.
是的,您可以像在普通 html 页面中一样使用它。在 php 脚本中使用包含在 echo 内的脚本。
You may use it outside the php script normally. Use container tag for the same in the php page.
您通常可以在 php 脚本之外使用它。在 php 页面中使用容器标签。
I hope it would help.
我希望它会有所帮助。