How to display HTML content in a textarea

Hi Guys,

What is the best way to show HTML source code within a textarea form field? For example:

<textarea name="template" />< html code here></textarea>

You could try something like this:


<textarea>
<?php 

// Pick the File (If Using One)
$file = 'sample.txt';

// Echo & Decode HTML Chars
echo htmlspecialchars_decode($file); 

?>
</textarea>

Hmmm, I do not think that will work in this instance as the HTML is stored in the database as is (we do not use htmlspecialchars) when inserting the HTML…


<textarea rows="500" cols="100">
<?php 

$file = "test.html";

echo file_get_contents($file); 

?>
</textarea>

Just remember that it is going to display as text in the textarea. If you want the HTML to be processed as HTML it can’t go in a textarea.

Wouldn’t you rather want to encode the HTML, unless you really, really, really trust what’s in the database?

Hmmmm, I am sure I have seen this done before, like in the admin area of vBulletin for editing templates. They successfully manage to display HTML code within a text box. I just need to know how it can be done… :slight_smile:

You can’t put non-entity encoded html in a textarea it must be entity encoded.

They were likely converted through Javascript into contenteditable iframed documents. The only thing textarea and text input elements will display is the raw text.