如何在 JavaScript 中嵌入 PHP 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3352576/
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
How do I embed PHP code in JavaScript?
提问by Amit Sharma
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.
请提出更好的方法。
回答by reko_t
If your whole JavaScript code gets processed by PHP, then you can do it just like that.
如果您的整个 JavaScript 代码都由 PHP 处理,那么您就可以这样做。
If you have individual .jsfiles, and you don't want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript.
如果您有单独的.js文件,并且您不希望 PHP 处理它们(例如,出于缓存原因),那么您可以只在 JavaScript 中传递变量。
For example, in your index.php(or wherever you specify your layout), you'd do something like this:
例如,在您的index.php(或您指定布局的任何地方)中,您将执行以下操作:
<script type="text/javascript">
var my_var = <?php echo json_encode($my_var); ?>;
</script>
You could then use my_varin your JavaScript files.
然后您可以my_var在您的 JavaScript 文件中使用。
This method also lets you pass other than just simple integer values, as json_encode()also deals with arrays, strings, etc. correctly, serialising them into a format that JavaScript can use.
此方法还允许您传递的不仅仅是简单的整数值,json_encode()还可以正确处理数组、字符串等,将它们序列化为 JavaScript 可以使用的格式。
回答by Sarfraz
If you put your JavaScript code in the PHPfile, 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);
}
回答by Natascha
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。或者,如果您知道这个文件主要是什么,您可以将其命名为 javascript.js.php - 服务器会将其识别为 .php 并对其进行解析。
回答by William Ausling
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");
}) ();
回答by Pablo Santa Cruz
Yes, you can, provided your JavaScript code is embedded into a PHP file.
是的,您可以,前提是您的 JavaScript 代码嵌入到 PHP 文件中。
回答by Oli
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>
回答by Zhang Evan
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>
回答by Bablu Ahmed
Here is an example:
下面是一个例子:
html_code +="<td>" +
"<select name='[row"+count+"]' data-placeholder='Choose One...' class='chosen-select form-control' tabindex='2'>"+
"<option selected='selected' disabled='disabled' value=''>Select Exam Name</option>"+
"<?php foreach($NM_EXAM as $ky=>$row) {
echo '<option value='."$row->EXAM_ID". '>' . $row->EXAM_NAME . '</option>';
} ?>"+
"</select>"+
"</td>";
Or
或者
echo '<option value=\"'.$row->EXAM_ID. '\">' . $row->EXAM_NAME . '</option>';
回答by Your Common Sense
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 HTMLone.
但是,我们可以使用所有 PHP 功能(包括从 HTML 中转义)生成 JavaScript 代码和HTML。

