简体   繁体   English

PHP函数中的JavaScript jQuery

[英]JavaScript jQuery inside PHP function

This is a theoretical question. 这是一个理论问题。 My question is whether a jQuery function or script can be written inside a PHP function. 我的问题是是否可以在PHP函数内编写jQuery函数或脚本。 Eg 例如

<?php function phpfunc(){
$a=10; ?>

<script>var a ='<?php $a ?>'</SCRIPT> <?php } ?>

Is this possible and legal? 这有可能合法吗?

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! 非常感谢!

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>";


} ?>

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. 请注意,如果你的变量碰巧有一个'在里面,你就会搞砸了。

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. 请注意,JavaScript部分中没有引号 - 如果需要, json_encode将添加引号。 This can be used to pass almost any kind of variable. 这可以用来传递几乎任何类型的变量。

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;回显到页面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

Yes you may use it like in normal html page. 是的你可以像普通的html页面一样使用它。 Use script include inside echo inside the php script. 在php脚本中使用脚本包含内部echo。

You may use it outside the php script normally. 您可以在php脚本之外正常使用它。 Use container tag for the same in the php page. 在php页面中使用容器标记。

I hope it would help. 我希望它会有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM