如何在JavaScript中嵌入PHP代码?

时间:2021-11-18 08:08:55

How can we use PHP code in JavaScript?

我们如何在JavaScript中使用PHP代码?

Like

喜欢

function jst()
{
    var i = 0;
    i = <?php echo 35; ?>
    alert(i);
}

Please suggest a better way.

请建议一个更好的方法。

8 个解决方案

#1


90  

If your whole JavaScript code gets processed by PHP, then you can do it just like that. If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript. For example, in your index.php (or wherever you specify your layout), you'd do something like this:

如果您的整个JavaScript代码都由PHP处理,那么您就可以这样做。如果您有单独的.js文件,并且您不希望PHP处理它们(例如,出于缓存原因),那么您只需在JavaScript中传递变量即可。例如,在index.php(或指定布局的任何位置)中,您可以执行以下操作:

<script type="text/javascript">
    var my_var = <?php echo json_encode($my_var); ?>;
</script>

You could then use my_var in your JavaScript files. This method also lets you pass other than just simple integer values, as json_encode() also deals with arrays, strings, etc. correctly, serializing them into a format that JavaScript can use.

然后,您可以在JavaScript文件中使用my_var。此方法还允许您传递除简单整数值之外的其他值,因为json_encode()也正确处理数组,字符串等,将它们序列化为JavaScript可以使用的格式。

#2


15  

If you put your JavaScript code in the PHP file, you can, but not otherwise. For example:

如果将JavaScript代码放在PHP文件中,则可以,但不是。例如:

page.php (this will work)

page.php(这会起作用)

function jst()
{
    var i = 0;
    i = <?php echo 35; ?>;
    alert(i);
}

page.js (this won't work)

page.js(这不起作用)

function jst()
{
    var i = 0;
    i = <?php echo 35; ?>
    alert(i);
}

#3


3  

PHP has to be parsed on the server. JavaScript is working in the client's browser.

必须在服务器上解析PHP。 JavaScript正在客户端的浏览器中工作。

Having PHP code in a .js file will not work, except you can tell the server to parse the file you want to have as .js before it sends it to the client. And telling the server is the easiest thing in the world: just add .php at the end of the filename.

在.js文件中使用PHP代码是行不通的,除非您可以告诉服务器在将文件发送到客户端之前解析您想要的文件.js。告诉服务器是世界上最简单的事情:只需在文件名末尾添加.php即可。

So, you could name it javascript.php. Or, so you know what this file is PRIMARILY, you could name it javascript.js.php - the server will recognize it as .php and parse it.

所以,你可以将其命名为javascript.php。或者,所以你知道这个文件是PRIMARILY,你可以将其命名为javascript.js.php - 服务器将其识别为.php并解析它。

#4


2  

Yes, you can, provided your JavaScript code is embedded into a PHP file.

是的,您可以,只要您的JavaScript代码嵌入到PHP文件中即可。

#5


2  

You're pretty much on the ball. The only difference is I'd separate out the JavaScript code so the majority was in an external static file. Then you just define variables or call a function from the actual PHP page:

你差不多了。唯一的区别是我将JavaScript代码分开,因此大多数都在外部静态文件中。然后你只需要从实际的PHP页面定义变量或调用一个函数:

<script type="text/javascript>
    function_in_other_file(<?php echo my_php_var; ?>);
</script>

#6


2  

This is the bit of code you need at the top of your JavaScript file:

这是您在JavaScript文件顶部需要的一些代码:

<?php
    header('Content-Type: text/javascript; charset=UTF-8');
?>

(function() {
    alert("hello world");
}) ();

#7


1  

A small demo may help you: In abc.php file:

一个小的演示可以帮助你:在abc.php文件中:

<script type="text/javascript">
    $('<?php echo '#'.$selectCategory_row['subID']?>').on('switchChange.bootstrapSwitch', function(event, state) {
        postState(state,'<?php echo $selectCategory_row['subID']?>');
    });
</script>

#8


-1  

We can't use "PHP in between JavaScript", because PHP runs on the server and JavaScript - on the client.

我们不能在JavaScript之间使用“PHP”,因为PHP在服务器和JavaScript上运行 - 在客户端上运行。

However we can generate JavaScript code as well as HTML, using all PHP features, including the escaping from HTML one.

但是,我们可以使用所有PHP功能生成JavaScript代码和HTML,包括从HTML转发。

#1


90  

If your whole JavaScript code gets processed by PHP, then you can do it just like that. If you have individual .js files, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript. For example, in your index.php (or wherever you specify your layout), you'd do something like this:

如果您的整个JavaScript代码都由PHP处理,那么您就可以这样做。如果您有单独的.js文件,并且您不希望PHP处理它们(例如,出于缓存原因),那么您只需在JavaScript中传递变量即可。例如,在index.php(或指定布局的任何位置)中,您可以执行以下操作:

<script type="text/javascript">
    var my_var = <?php echo json_encode($my_var); ?>;
</script>

You could then use my_var in your JavaScript files. This method also lets you pass other than just simple integer values, as json_encode() also deals with arrays, strings, etc. correctly, serializing them into a format that JavaScript can use.

然后,您可以在JavaScript文件中使用my_var。此方法还允许您传递除简单整数值之外的其他值,因为json_encode()也正确处理数组,字符串等,将它们序列化为JavaScript可以使用的格式。

#2


15  

If you put your JavaScript code in the PHP file, you can, but not otherwise. For example:

如果将JavaScript代码放在PHP文件中,则可以,但不是。例如:

page.php (this will work)

page.php(这会起作用)

function jst()
{
    var i = 0;
    i = <?php echo 35; ?>;
    alert(i);
}

page.js (this won't work)

page.js(这不起作用)

function jst()
{
    var i = 0;
    i = <?php echo 35; ?>
    alert(i);
}

#3


3  

PHP has to be parsed on the server. JavaScript is working in the client's browser.

必须在服务器上解析PHP。 JavaScript正在客户端的浏览器中工作。

Having PHP code in a .js file will not work, except you can tell the server to parse the file you want to have as .js before it sends it to the client. And telling the server is the easiest thing in the world: just add .php at the end of the filename.

在.js文件中使用PHP代码是行不通的,除非您可以告诉服务器在将文件发送到客户端之前解析您想要的文件.js。告诉服务器是世界上最简单的事情:只需在文件名末尾添加.php即可。

So, you could name it javascript.php. Or, so you know what this file is PRIMARILY, you could name it javascript.js.php - the server will recognize it as .php and parse it.

所以,你可以将其命名为javascript.php。或者,所以你知道这个文件是PRIMARILY,你可以将其命名为javascript.js.php - 服务器将其识别为.php并解析它。

#4


2  

Yes, you can, provided your JavaScript code is embedded into a PHP file.

是的,您可以,只要您的JavaScript代码嵌入到PHP文件中即可。

#5


2  

You're pretty much on the ball. The only difference is I'd separate out the JavaScript code so the majority was in an external static file. Then you just define variables or call a function from the actual PHP page:

你差不多了。唯一的区别是我将JavaScript代码分开,因此大多数都在外部静态文件中。然后你只需要从实际的PHP页面定义变量或调用一个函数:

<script type="text/javascript>
    function_in_other_file(<?php echo my_php_var; ?>);
</script>

#6


2  

This is the bit of code you need at the top of your JavaScript file:

这是您在JavaScript文件顶部需要的一些代码:

<?php
    header('Content-Type: text/javascript; charset=UTF-8');
?>

(function() {
    alert("hello world");
}) ();

#7


1  

A small demo may help you: In abc.php file:

一个小的演示可以帮助你:在abc.php文件中:

<script type="text/javascript">
    $('<?php echo '#'.$selectCategory_row['subID']?>').on('switchChange.bootstrapSwitch', function(event, state) {
        postState(state,'<?php echo $selectCategory_row['subID']?>');
    });
</script>

#8


-1  

We can't use "PHP in between JavaScript", because PHP runs on the server and JavaScript - on the client.

我们不能在JavaScript之间使用“PHP”,因为PHP在服务器和JavaScript上运行 - 在客户端上运行。

However we can generate JavaScript code as well as HTML, using all PHP features, including the escaping from HTML one.

但是,我们可以使用所有PHP功能生成JavaScript代码和HTML,包括从HTML转发。