无法将 PHP 文件加载到使用 javascript 动态创建的内部 HTML Div 中

发布于 2024-12-09 21:26:53 字数 1994 浏览 0 评论 0原文

我只需使用 即可将 .php 文件加载到 div 中 该文件可以包含 php、javascript、html 和纯文本,但它仍然可以很好地工作。

如果文件中有脚本标签,我无法将文件加载到我用 javascript 动态创建的 Div 中。

注意:我会在取得进展时更新代码

index.php

第一个 Div 运行良好,加载 test.php 文件没有任何问题。

<div id="phpDiv"
    style="
    position:absolute; 
    top: 50px; 
    left: 50px;
    height: 200;
    width: 300;
    background-color: #CCCCCC;"> 
        This is the phpDiv  <br>
        <?php include("test.php"); ?>
</div>


动态创建的第二个 Div 如果其中包含脚本标记,则不会加载该文件

<script>
    var javascriptDiv = document.createElement('div');
    javascriptDiv.setAttribute('id', 'javascriptDiv');
    javascriptDiv.style.position = 'absolute';  
    javascriptDiv.style.top = 350;
    javascriptDiv.style.left = 50;
    javascriptDiv.style.height = 200;
    javascriptDiv.style.width = 300;
    javascriptDiv.style.background = '#CCCCCC'; //so we can see that the DIV was created

    <?php  
            //must have http:// in it to load php stuff
            //will not load files with <script> tags
            $file = file_get_contents('http://127.0.0.1/Debug/test.php');

            //Trim the string and remove new lines.
            $file = trim($file);
            $file = str_replace("\n", "", $file);
            $file = str_replace("\r", "", $file);

            echo "javascriptDiv.innerHTML = \"This is the javascriptDiv <br>\";  ";  
            echo "javascriptDiv.innerHTML += '". $file ."';";
    ?>

    document.body.appendChild(javascriptDiv); //Display the Window
</script>

test.php <-我正在尝试加载的文件

<?php echo "php works <br>"; ?>
<script> alert('javascript works'); </script>
<a> html works </a><br><br>

and extra spaces and plain text work fine as well 

I am able to load a .php file into a div simply by using <?php include("file.php") ?>
The file can have php, javascript, html and plain text in it and it still works great.

I am NOT able to load the file into a Div I created dynamically with javascript if the file has script tags in it.

NOTE: I am updating the code as I make progress

index.php

The first Div works great and loads the test.php file with no issues.

<div id="phpDiv"
    style="
    position:absolute; 
    top: 50px; 
    left: 50px;
    height: 200;
    width: 300;
    background-color: #CCCCCC;"> 
        This is the phpDiv  <br>
        <?php include("test.php"); ?>
</div>

The Second Div created dynamically will not load the file if it has script tags in it

<script>
    var javascriptDiv = document.createElement('div');
    javascriptDiv.setAttribute('id', 'javascriptDiv');
    javascriptDiv.style.position = 'absolute';  
    javascriptDiv.style.top = 350;
    javascriptDiv.style.left = 50;
    javascriptDiv.style.height = 200;
    javascriptDiv.style.width = 300;
    javascriptDiv.style.background = '#CCCCCC'; //so we can see that the DIV was created

    <?php  
            //must have http:// in it to load php stuff
            //will not load files with <script> tags
            $file = file_get_contents('http://127.0.0.1/Debug/test.php');

            //Trim the string and remove new lines.
            $file = trim($file);
            $file = str_replace("\n", "", $file);
            $file = str_replace("\r", "", $file);

            echo "javascriptDiv.innerHTML = \"This is the javascriptDiv <br>\";  ";  
            echo "javascriptDiv.innerHTML += '". $file ."';";
    ?>

    document.body.appendChild(javascriptDiv); //Display the Window
</script>

test.php <-the file i'm trying to load

<?php echo "php works <br>"; ?>
<script> alert('javascript works'); </script>
<a> html works </a><br><br>

and extra spaces and plain text work fine as well 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神回复 2024-12-16 21:26:53

我想说它不起作用,因为你在设置innerHTML的单引号语句中使用了单引号。

<?php echo "php works <br>"; ?>
<script> alert("javascript works"); </script>
<a> html works </a><br><br>

编辑:

您可以尝试自动删除换行符,如下所示:

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('test.php', false, $context);
$file = str_replace( "\r\n", "", $file );

将 $file 放入您的innerHTML中

I would say it doesn't work because you use single quotes into your single quotes statement setting your innerHTML.

<?php echo "php works <br>"; ?>
<script> alert("javascript works"); </script>
<a> html works </a><br><br>

Edit:

You can try to strip newlines automatically like so:

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('test.php', false, $context);
$file = str_replace( "\r\n", "", $file );

put $file into your innerHTML

只有一腔孤勇 2024-12-16 21:26:53

尝试看看这段代码。我成功了。创建您的index.php代码并将Jquery文件放入该目录中。从这里下载它 http://jquery.com/download /

<html lang="en">
<head>
<meta charset="utf-8">
<title>load demo</title>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="jquery.js"></script>
</head>
<body>
<b>Successful Response (should be blank):</b>
<div id="success"></div>
<b>Error Response:</b>
<div id="error"></div>
<script>
var browser=navigator.userAgent;
//if (browser.lastIndexOf("Chrome")>=5)
{
alert("ok");
$("#success").load("index.php", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
}
</script>
</body>
</html>

Try to see this code . I was successful with it . Create your index.php code and put the Jquery file in that directory.Download it from here http://jquery.com/download/.

<html lang="en">
<head>
<meta charset="utf-8">
<title>load demo</title>
<style>
body{ font-size: 12px; font-family: Arial; }
</style>
<script src="jquery.js"></script>
</head>
<body>
<b>Successful Response (should be blank):</b>
<div id="success"></div>
<b>Error Response:</b>
<div id="error"></div>
<script>
var browser=navigator.userAgent;
//if (browser.lastIndexOf("Chrome")>=5)
{
alert("ok");
$("#success").load("index.php", function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
$("#error").html(msg + xhr.status + " " + xhr.statusText);
}
});
}
</script>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文