Javascript 如何在javascript中嵌入php脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8227638/
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 to embed php script in javascript?
提问by puk
I want to know if I can include php
scripts in javascript
[1]the same way it can be done in html
[2]
我想知道我是否可以像在[2] 中那样在[ 1] 中包含php
脚本javascript
html
server.php:
<?php
echo 'hello World';
?>
client.js (or client.php if necessary):
client.js(或 client.php 如有必要):
function foo() {
var i = <?php include('server.php'); ?>
console.log( i );
}
If it can be done, and if client.php
must be used instead, I would really appreciate a minimal example with index.html/index.php
as well, since I would like to see how I would include client
now that it is a php
file and not javascript
如果可以完成,并且client.php
必须改为使用,我真的很感激一个最小的例子index.html/index.php
,因为我想看看我client
现在如何包含它是一个php
文件而不是javascript
回答by nickb
You can use output buffering to achieve something similar, although it's not possible to directly embed PHP code in JS.
您可以使用输出缓冲来实现类似的功能,尽管无法将 PHP 代码直接嵌入到 JS 中。
Example:
例子:
server.php
服务器.php
<?php
echo 'hello World';
?>
client.php- (.php extension enables the ability to parse PHP tags, but really outputs JS)
client.php- (.php 扩展名能够解析PHP标签,但真正输出JS)
<?php
header("Content-type: application/x-javascript"); // Or 'text/javascript'
ob_start();
include( 'server.php');
$i = ob_get_contents();
ob_end_clean();
?>
function foo() {
var i = '<?= json_encode( $i); ?>';
console.log( i );
}
Edit:If the server.phpfile will only return a simple string, then you can modify your code for client.php. Notice how I said "return" instead of output - If your server.phpoutputs anything, it will be sent to the browser as output (not what you want). Alternatively, you can set a variable in server.phpwhich gets outputted in client.php, or encapsulate your code in a function:
编辑:如果server.php文件只返回一个简单的字符串,那么您可以修改client.php的代码。请注意我是如何说“返回”而不是输出的 - 如果您的server.php输出任何内容,它将作为输出发送到浏览器(不是您想要的)。或者,您可以在server.php 中设置一个变量,该变量在client.php 中输出,或者将您的代码封装在一个函数中:
server.php
服务器.php
<?php
function js_output()
{
return 'hello world';
}
?>
client.php
客户端.php
<?php
header("Content-type: application/x-javascript"); // Or 'text/javascript'
?>
function foo() {
var i = '<?php include( 'server.php'); echo addslashes( js_output()); ?>';
console.log( i );
}
Note: You may also want to add a call to html_entities
or htmlspecialchars
.
注意:您可能还想添加对html_entities
或的调用htmlspecialchars
。
回答by Rene Pot
It has to be a .php file. You need to set this as the first line in your client.php:
它必须是一个 .php 文件。您需要将其设置为 client.php 中的第一行:
header("Content-type: text/javascript");
Then, you could create the file like that, and just include it, and use it as a regular javascript file. So with the <script>
tag, as it is now recognized as JavaScript
然后,您可以像这样创建文件,只需包含它,并将其用作常规 javascript 文件。所以使用<script>
标签,因为它现在被识别为 JavaScript
回答by Rsaesha
The easiest thing to do would be to use client.php instead of client.js, since your server will automatically send a .php file through the PHP interpreter. You technically could reconfigure the server to do it to .js files, but I wouldn't recommend it.
最简单的做法是使用 client.php 而不是 client.js,因为您的服务器将通过 PHP 解释器自动发送一个 .php 文件。从技术上讲,您可以重新配置服务器以将其处理为 .js 文件,但我不建议这样做。
The client.php file should include this line before anything is outputted to the user:
在向用户输出任何内容之前,client.php 文件应该包含这一行:
header("Content-type: text/javascript");
As for a basic index.html/index.php example:
至于基本的 index.html/index.php 示例:
<html>
<head>
<title>Example</title>
<script type="text/javascript" src="client.php"></script>
</head>
<body>
...
</body>
</html>
Update:example client.php file:
更新:示例 client.php 文件:
<?php
header("Content-type: text/javascript"); // This bit must come first!
?>
var i = "<?php echo $somevar; ?>" // This is JavaScript code with some PHP embedded.
回答by brainchild
To embed PHP in a file the server needs to pass it through the PHP parser and it will only do that with files-types it has been told contain PHP markup, i.e. .php files. You could potentially tell the server to parse js-files as well, but I'm not sure if the mime-type would be set correctly. It would also require you to change the server settings. If you call the javascript-file .php the mime-type will definitely be wrong. The most compatible way to do this would be to put the javascript in the html-file (i.e. your .php file). If you want to keep the amount of javascript in your .php files to a minimum you can just put the variable-assignments in the .php file and keep the rest in your javascript file.
要将 PHP 嵌入到文件中,服务器需要通过 PHP 解析器传递它,并且它只会对已被告知包含 PHP 标记的文件类型(即 .php 文件)执行此操作。您也可以告诉服务器解析 js 文件,但我不确定 mime-type 是否设置正确。它还需要您更改服务器设置。如果你调用 javascript-file .php mime-type 肯定是错误的。最兼容的方法是将 javascript 放在 html 文件(即您的 .php 文件)中。如果您想将 .php 文件中的 javascript 数量保持在最低限度,您可以将变量赋值放在 .php 文件中,并将其余部分保留在您的 javascript 文件中。
server.php:
服务器.php:
<script language="javascript">
var some_var = "<?php echo $SOME_VAR ?>";
</script>
<script src="client.js" type="text/javascript">
client.js:
客户端.js:
alert(some_var);
The (global) javascript variable some_var
will be set to whatever the PHP variable $SOME_VAR
is set to. Don't forget the quotes when setting string values. Also if the PHP code contains errors you could contaminate the javascript code so alot of care has to be taken when doing this.
(全局)javascript 变量some_var
将设置为 PHP 变量$SOME_VAR
设置的任何值。设置字符串值时不要忘记引号。此外,如果 PHP 代码包含错误,您可能会污染 javascript 代码,因此在执行此操作时必须非常小心。
回答by Dah Moymy
I tested this solution on PHP 5.5.
1. Create a file called myScript.php.
我在 PHP 5.5 上测试了这个解决方案。
1. 创建一个名为 myScript.php 的文件。
// myScrpt.php content:
// myScrpt.php 内容:
<?php $fname = "Jason"; $lname = "Kent"; ?>
<?php $fname = "Jason"; $lname = "Kent"; ?>
- Create a file called test.php.
- 创建一个名为 test.php 的文件。
// test.php content:
// test.php 内容:
<!DOCTYPE html> <html> <head> <title>Include php Script</title> <script type="text/javascript"> <?php include 'myScript.php'; ?> </script> </head>
<!DOCTYPE html> <html> <head> <title>Include php Script</title> <script type="text/javascript"> <?php include 'myScript.php'; ?> </script> </head>
<h1>The result is:</h1> <?php echo "My full name is $fname $lname."; ?> </body> </html>
<h1>The result is:</h1> <?php echo "My full name is $fname $lname."; ?> </body> </html>
- When you open test.php in a browser, you will get:
- 当你在浏览器中打开 test.php 时,你会得到:
My full name is Jason Kent.
我的全名是杰森·肯特。
回答by puk
You cannot have a client load PHP. The only way you can do this is to have on server.php
您不能让客户端加载 PHP。你可以做到这一点的唯一方法是在 server.php
<html>
<head>
<script type="text/javascript">
function foo() {
var i = <?php echo 'hello World'; ?>
console.log( i );
}
</script>
</head>
<body>
</body>
</html>
Basically loading the javascript into the body of the page. Or alternatively.
基本上是将 javascript 加载到页面正文中。或者换一种方式。
<html>
<head>
<script type="text/javascript">
var i = <?php echo 'hello World'; ?>
</script>
<script type="text/javascript" src="client.js">
</head>
<body>
</body>
</html>
And in client.js
在 client.js 中
function foo() {
console.log( i );
}
essentially variable i can be loaded as a global variable on the page
本质上变量 i 可以作为页面上的全局变量加载
or as another post suggest, masquerade a php file as a js.
或者正如另一篇文章所建议的那样,将一个 php 文件伪装成一个 js。