Question

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?

Was it helpful?

Solution

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 />

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top