How can I change innerHTML with PHP?

innerhtml, php

Solution

There are many small typos. Try removing the space between $ and 'home' and before 'php'. This is the right statement:

document.getElementById ("page"). innerHTML = "<?php echo $home?>";

Also, where's your closing php tag?

<?php
$home = file_get_contents("home.php");
?>
<script type="text/javascript">
    function ChangePage(page)
    {
       if(page == "home")
            {
            document.getElementById("page").innerHTML = "<?php echo $home; ?>";
            }
        }
</script>

Although this is a bad practice. Why would you want to do this instead of simply loading the php in the right place? Also, you do realize that 'page' should be the id of a pre-existing div in your html, right? Something like this would be better:

<html>
  <body>
    <div id = "page">
      <?php echo file_get_contents("home.php"); ?>
    </div>
  </body>
</html>

Problem

I want to change the innerHTML with PHP code. But I cannot get it to work, and I do not understand why. I want it to change some text on the page but from another file. And so I thought that I could use this: ``` document.getElementById ("page"). innerHTML = "<? php echo $ home?>"; ``` But it does not work. Here is my code: ``` <?php $home = file_get_contents("home.php"); ?> <script type="text/javascript"> function ChangePage(page) { if(page == "home") { document.getElementById("page").innerHTML = "<?php echo $home ?"; } } </script> ```

Original source

Related problems