Reputation: 81
I am trying to get the value inside a div with php Dom get element by id, when I try to do so the value that I get is ' games and new games releases ' from the div and I dont get the php value and the php tags at all eg. < ? php echo hellow world as show below. I would like to get all the contents of the div inside the variable $existing_Data, including < ? php and < h1 > etc..
$selected_div = "div_footer";
$file = '../myfolder/22selected.php';
$file_get = file_get_contents($file);
/* // which one to use here ? i am getting a headache !
$file_entity = htmlentities($file_get);
$file_htmlcharac = htmlspecialchars($file_entity);
$file_strip = stripslashes($file_htmlcharac);
*/
$doc = new DOMdocument();
$doc->loadHTML($file_get);
$element = $doc->getElementById($selected_div);
$existing_Data = $element->firstChild->nodeValue;
echo '<table>';
echo '<form action= "existing_Data.php" method = "POST">';
echo 'Text Content <textarea rows="12" cols="60" name="content" id ="text_content">' . $existing_Data . '</textarea>';
echo '<input type ="submit" name = "submit" id = "submit" value = "Submit" ></form>';
// div_footer CONTENTS
<div id = "div_footer">
<h1> All games</h1>
<p> new games releases</p>
<?php echo "hello world"; ?>
</div>
Upvotes: 1
Views: 539
Reputation: 32270
From what you show, we cannot guess what you want to achieve.
When you open a file using file_get_contents()
without a scheme (http, ftp, ..), PHP tries to open the file through the local filesystem and so the tags are not getting interpreted by PHP. The downside is, it's not valid XML/HTML and so a DOM parser won't help.
You should write the data into a database or at least an XML, JSON or plaintext file. To get the value back, all you need to do is $value = trim(file_get_contents('../mydata.txt'));
.
The other scenario is, you want to (http-)POST that data. Then there is no reason to use a DOM parser either. Simply put the data into a hidden field <input type="hidden" name="data" value="<?php echo $data; ?>
.
To prevent different (not all) kinds of XSS, see
htmlentities() vs. htmlspecialchars()
How to prevent XSS with HTML/PHP?
I strongly suggest to read this QA:
What are the differences between server-side and client-side programming?
Upvotes: 1