views:

55

answers:

2

I'm populating a textarea with previous input of a user. This is pulled from a database and set as the content of the textarea server side.

It seems we are having an issue with a typo and a combination of special characters. if the user inputs &#6 originally, when I try to populate my textarea with that it just renders a little square like its interpreting the character encoded value.

Creating a HTML file with the following demonstrates my issue.

<textarea name"mytextarea">some text &#5 some more text </textarea

this is a typo, the user intended to enter #5 & #6 so a fix for this is simply to ensure when the user puts an ampersand in that I have a space on either side of it before I display it in the textarea. Its just a special character issue backwards from what i'm use to seeing.

I was just curious if there is a way to get the text area to display the characters like the user typed it and preserve that through form submission. To save the over head of having to parse or html encode the text before putting into the textarea.

Thanks, Muchly

+2  A: 

Inside a textarea, you need to convert the following characters into their HTML entities:

& => &amp;
> => &gt;
< => &lt;

That way, &#5 would become &amp;#5. Visually, to the user, it would remain &#5.

You are not specifying the server side language you're using. In PHP, the correct function would be htmlspecialchars()

Pekka
I'll try to set up a test to verify this but if converted to html enttities then copied to another textarea via javascript then submitted would the page processing the form receive or $amp;#6 ? That is where I'm likely to run into an issue.
Nathan
If your HTML contains `<textarea></textarea>`, then the user would see a textbox that contains the text ``, and if they submit, the server would receive the text ``. That's just how you have to specify an ampersand character in your page, as opposed to an escape character that begins an HTML entity.
Joe White
A: 

escape the & as &amp;

joni