Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I need to put up a simple form to edit HTML that is stored in a database. I read in the HTML and then display it in a textarea box.

It works fine except for HTML characters. If "& amp;" is stored in the database, then "&" gets displayed in the textarea box.

When I save the form, "&" gets saved instead of "& amp;". This happens for all characters not just the ampersand.

Is their a way to have the textarea box not render the characters, or is their some alternative?

thanks

Replies are listed 'Best First'.
Re: Textarea to edit HTML?
by ikegami (Patriarch) on Mar 03, 2011 at 23:17 UTC

    You want to treat the contents of the database as text, so you need to consider it text. As such, you need to convert it to HTML by escaping anything HTML might find interesting (using encode_entities).

    For example, the HTML for "Rock & Roll" is

    Rock & Roll
    The HTML for a textarea initialised to that string is
    <textarea>Rock &amp;amp; Roll</textarea>
Re: Textarea to edit HTML?
by NetWallah (Canon) on Mar 04, 2011 at 06:08 UTC
    The CGI module provides Autoescaping and escapeHTML functions for just this purpose.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis