2

How can i enable a Break Line or New Line in Textarea when i press the Enter Key ? So that if I press the ENTER it saves in the form of New Line in database.

I have already searched some topics but it hasn't helped me.

Here is my Textarea Code:

<textarea name="post" id="post"  class="valid Post_Text_Area" placeholder="Write something here..." ></textarea>

I tried this JS Code so far. but it hasn't worked for me

JavaScript: How to add line breaks to an HTML textarea?

Community
  • 1
  • 1
Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92

2 Answers2

6

Newlines in textarea's are saved into the database by default.

To generate them as Html BR codes, use nl2br($yourTextVariable) $yourTextVariable will be the variable you fetch from database. so use it like this :

<?php echo nl2br($yourTextVariable); ?>

This transforms newline to <br />

Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92
Tikkes
  • 4,599
  • 4
  • 36
  • 62
0

Another solution is put the output into <pre> tag or just style it with css white-space: pre. It will not swallow multiple spaces.

kan
  • 28,279
  • 7
  • 71
  • 101
  • `
    `. (Yes, don't forget to use `htmlspecialchars` or `strip_tags` if you don't want to allow http://en.wikipedia.org/wiki/Cross-site_scripting).
    – kan Sep 10 '13 at 08:38