How to save user-entered line breaks from a TextArea to a database?

How do I save user-entered line breaks from a <textarea> HTML element to a database?
It always removes the line breaks.


Solution 1:

TextArea HTML element is preserving the white space in the database.
The problem appears when trying to display the \n on the web browser, which will fail.

To display \n in the web browser use :

<p style="white-space: pre-line">multi-line text</p>

Solution 2:

When displaying the content you need to convert line breaks into <br /> tags, otherwise the web browser won't display them. This is probably why you think they aren't being saved. If you're using PHP, use the nl2br() function to do this. In other languages you could do a string replace, replacing all occurrences of "\n" with "<br />".

Solution 3:

Just put the output text between <pre></pre> tags, that will preserve the line breaks.