简体   繁体   中英

Textarea: pressing enter creates new line

When I press enter in the textarea it goes to a new line, but that new line does not show up when the textarea is submitted. How do I make this work? It should be simple, but from what I have searched and tried has failed.

<textarea style="{ white-space:pre; }"; name="texts" rows="3" cols="40"></textarea>

$texts = $_POST['texts'];

I think you're looking for the nl2br() function.

nl2br()

Probably the issue is that the new line is showing up, but you're outputing it as html, so it doesn't conserve that new line. You need to convert newlines to break tags when you output as html. You can do it like so:

//editing per comment advice. Apparently this method shouldn't be used since it doesn't replace all possible newline representations, although I dont remember having an issue with it.
//$string = str_replace("\n", "<br />", $string);
//as others mentioned, this is better and easier:
$string = nl2br($string);

Perhaps I'm misunderstanding the question, but this is what I took from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM