in reply to HTML Problem getting entities into a textarea

When you're emiting the textarea tags, escape the entities in the content. It appears to suffice to escape '&' and '<', though throwing in '>' won't hurt.

Conceptually, this looks like

my $escaped = $content; $escaped =~ s/&/&amp;/g; $escaped =~ s/</&lt;/g; $escaped =~ s/>/&gt;/g; print "<textarea>$escaped</textarea>";

Replies are listed 'Best First'.
Re: Re: HTML Problem getting entities into a textarea
by pinetree (Scribe) on Apr 19, 2003 at 04:53 UTC

    And make sure you escape the & before the < and > otherwise you end up with something along the lines of:

    "<" -> "%lt;" -> "&lt;"

Re: Re: HTML Problem getting entities into a textarea
by muad33b (Acolyte) on Apr 18, 2003 at 21:09 UTC

    See my above note on this idea. The problem is that I want folks to be able to create a technote (in a textread form) that contains any html tag, and save it exactly as typed, and then have them be able to edit that exact text again later, without some of those tag being interpreted as tags for the page instead of text in the textarea. If I modify the content like that, then a user types in one thing and ends up editing another.

    Then again, if I told folks not to escape, and I escaped for them before saving the file, then that just might work... then any tags would be saves with escapes, and displayed as text and not interpreted. The only problem with this is if you type, say, <pre> </pre> inside a textarea - that doesn't get interpreted, so I would be escaping things that don't need it, but I suppose that might be alright.

    Actually, that get ugly, because, then all of the tags that I am telling people to use to format the text of the technote will now not be tags that format the text when displayed later in a table, they will show as text, unless I unescape all before displaying to the table.

    That get's messy, but it may be the only way...

    Any other ideas?

      The problem is that I want folks to be able to create a technote (in a textread form) that contains any html tag, and save it exactly as typed, and then have them be able to edit that exact text again later, without some of those tag being interpreted as tags for the page instead of text in the textarea.

      From the sound of it, someone is doing some unecessary entity unescaping when the form is submitted. If a user types

      <foo>
      but what ends up saved is
      &lt;foo&gt;
      then there's a problem in your form processing logic.