簡體   English   中英

無法將PHP文件加載到使用javascript動態創建的內部HTML Div中

[英]Unable to load PHP file into a inner HTML Div created dynamically with javascript

我可以簡單地使用<?php include("file.php") ?>將.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>


如果動態創建的Second 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 

我會說這是行不通的,因為您在設置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

嘗試查看此代碼。 我成功了。 創建您的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM