homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

keeping HTML source in a form
keeping HTML source in a table

10+ Year Member

Msg#: 3666748 posted 2:57 pm on Jun 4, 2008 (gmt 0)

I have HTML source code in a DB. I built a small page to edit fields in the DB.

When I display the contents from the database into a textarea of a form I see the encoding, not the HTML source.

for example, I see < instead of &lt;

How do I call the source code into the <textarea> and have it display it as &lt; v. <





WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 3666748 posted 3:25 pm on Jun 4, 2008 (gmt 0)

I do this by programming.

Substitute when it comes out

$texfield =~ s/</\&lt\;/ig;
$texfield =~ s/>/\&gt\;/ig;

and convert it back when it goes in:

$texfield =~ s/\&lt\;/</ig;
$texfield =~ s/\&gt\;/>/ig;

So that when it's called from a "public" page, it displays as expected, yet doesn't screw up the form when you edit it.


WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

Msg#: 3666748 posted 3:26 pm on Jun 4, 2008 (gmt 0)

I think you'll need to essentially 'double-encode' your html entities before outputting to the textarea. Essentially converting all '&' into '&amp;'. However, there is a bit of a cross-browser issue if you have html entities within html comments - a problem I had a short while back... see this thread for more info and my post at the end for an explanation of your problem...

Regular expression - How to replace all &'s within an HTML comment [webmasterworld.com]
(an HTML source code editor...) The HTML content is loaded into a <textarea> to be edited. HTML entities, however, get converted by the browser into the actual characters for display so the HTML entity is lost. So &lt; is converted into &amp;lt; so that it actually displays in the textarea as &lt;. However, I've found that in IE and FF HTML entities that appear within HTML comments do not get converted into the actual characters, they stay as HTML entities (hence this thread), except in Opera they do (hhmmmm)!

(I think rocknbil types quicker than me ;)

Just to note... If you are allowing your users to enter HTML source to be saved as is (including entities), you probably don't need to 'convert this back' before saving? Or are there special cases?
eg. &lt; gets converted to &amp;lt; before writing it out to your page server-side (between <textarea> tags). This displays correctly as &lt; on the client side and this should get returned as-is to get plugged back into your database.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved