I have the main document created with DOM server-side using PHP. And I have to include some pieces of PHP/HTML without DOM. Example. There is file form.php containing a form
<form action="" method="post">
<input type="text" name="firstname" id="firstnameid" /><br/>
<input type="submit" name="submit" value="Submit" />
</form>
I want to place that form into a <DIV>
created using DOM in the file index.php. The DIV inside depends on other logic (condition $form_is_needed
)
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="utf-8" /> </head>
<body>
<?php
$form_is_needed = true;
$dom = new DOMDocument();
$domDiv = $dom->createElement("div");
if ($form_is_needed) {
// trying load not DOM
$str = file_get_contents('form.php');
$domDivText = $dom->createTextNode( html_entity_decode( $str ) );
$domDiv->appendChild($domDivText);
}
else {
$domDivText = $dom->createTextNode( "There is no form." );
$domDiv->appendChild($domDivText);
}
$dom->appendChild($domDiv);
$html = $dom->saveHTML();
echo $html;
?>
</body>
</html>
I get my content inside DIV but the escape characters has been added
<div><form action="" method="post">
<input type="text" name="firstname" id="firstnameid" /><br/>
<input type="submit" name="submit" value="Submit" />
</form>
</div>