如何在JavaScript中嵌入PHP脚本? [英] How to embed php script in javascript?

查看:461
本文介绍了如何在JavaScript中嵌入PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在 javascript 中包含 php 脚本 [1] 可以在 html [2]

  server.php:
<?php
echo'hello World';
?>

client.js(或者client.php,如果需要的话):

  function foo(){
var i =<?php include('server.php'); ?>
console.log(i);

$ / code>

如果可以完成,并且客户端。 php 必须用来代替,我真的很感激 index.html / index.php 的一个小例子,因为我希望看到我现在如何包含客户端现在它是 php 文件而不是 javascript

示例: server.php

 <?php 
echo'hello World';
?>

client.php - (.php扩展使得能够解析PHP标签,但真的输出JS)

 <?php 
header(Content-type:application / x- JavaScript的); //或'text / javascript'
ob_start();
include('server.php');
$ i = ob_get_contents();
ob_end_clean();
?>
函数foo(){
var i ='<?= json_encode($ i); ?>;
console.log(i);

编辑
如果 server.php 文件只会返回一个简单的字符串,那么您可以修改 client.php 的代码。注意我如何说return而不是输出 - 如果你的 server.php 输出任何内容,它将作为输出发送给浏览器(不是你想要的)。或者,您可以在 server.php 中设置一个变量,该变量在 client.php 中输出,或者将代码封装在一个函数中:

server.php

 <?php 
函数js_output ()
{
return'hello world';
}
?>

client.php

 <?php 
header(Content-type:application / x-javascript); //或'text / javascript'
?>
函数foo(){
var i ='<?php include('server.php'); echo addslashes(js_output()); ?>;
console.log(i);
}

注意:您可能还想添加对 html_entities htmlspecialchars


I want to know if I can include php scripts in javascript[1] the same way it can be done in html[2]

server.php:
<?php
    echo 'hello World';
?>

client.js (or client.php if necessary):

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

解决方案

You can use output buffering to achieve something similar, although it's not possible to directly embed PHP code in JS.

Example:

server.php

<?php
    echo 'hello World';
?>

client.php - (.php extension enables the ability to parse PHP tags, but really outputs 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.php file 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.php outputs anything, it will be sent to the browser as output (not what you want). Alternatively, you can set a variable in server.php which gets outputted in client.php, or encapsulate your code in a function:

server.php

<?php
function js_output()
{
    return 'hello world';
}
?>

client.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.

这篇关于如何在JavaScript中嵌入PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆