简体   繁体   中英

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

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 

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM