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>
php function DOMinnerHTML(DOMNode $element) { $innerHTML = ""; $children = $element->childNodes; foreach ($children as $child) { $innerHTML . = $element->ownerDocument->saveHTML($child); } return $innerHTML; } ?>
The innerHTML() function presented in this page it is the equivalent of the innerHTML property from JavaScript. This function can be used in PHP to get the HTML content from a HTML element within a DOMDocument object.
Answer: Use the jQuery html() Method You can simply use the jQuery html() method to replace innerHTML of a div or any other element.
Using the innerHTML attribute: To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With