2

Possible Duplicate:
How to save user-entered line breaks from a TEXTAREA to a database?

Am I missing some simple solution here - I am creating a form with a text input. I want this to be submitted WITH any linebreaks. The user actually writes in a textarea, which javascript/jquery copies to the hidden text input before submitting.

I have seen solutions where javascript replaces /n, etc. with some known break-character. Is this the only way to go? Then I have to translate it back again serverside. I would really like for /n to be submitted as /n.

Community
  • 1
  • 1
user984003
  • 28,050
  • 64
  • 189
  • 285

2 Answers2

4

You could use a <textarea style="display:none;"></textarea> instead of a hidden <input type="text">.

andytuba
  • 187
  • 9
  • yes! Thank you. Didn't realize that a textarea could be submitted in a form. Thought it had to be input. I gave it a name attribute so I could grab it serverside from my POST. And now it can be used directly (so is displayed) – user984003 Oct 11 '12 at 20:30
  • Yep. Forms will submit values from `input`, `textarea`, `select`, unless they're disabled or some other edge cases. http://www.w3.org/TR/html401/interact/forms.html#successful-controls A fun trick is to add multiple submit buttons and assign different values to them; the value of the clicked button, but not the other submit buttons, gets sent with the form data. – andytuba Oct 11 '12 at 20:35
2

When a normal form with a normal textarea is submitted, any line breaks entered by the user are transmitted as part of the data. They are canonicalized to CR LF pairs. The line breaks are preserved if you copy the value of a textarea element to a hidden field.

So the issue is elsewhere. Possibly your server-side form handler is not prepared to deal with the line breaks. Or perhaps it uses the data in a way that makes the line break lose significance, or get entirely lost. A common novice mistake is to write the value received from a form into an HTML document – even though line breaks are preserved, they are taken as equivalent to spaces by HTML rules, when appearing as content in a normal element (as opposite to e.g. textarea or pre).

Any line breaks created by automatic line wrapping by the browser, not by user actions, are lost by default. There is normally no reason to have them preserved, but should you wish to do so, use the attribute wrap=hard in the textarea element. (The attribute is nonstandard but widely supported.=

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390