I have a textarea form in my html. If the user hits enter between 2 sentences that data should be carried over to my PHP.
Currently if the user enters:
Apple
Google
MS
and my PHP code is:
$str = $_POST["field"];
echo $str;
I get
Apple Google MS
as the output. I want output to be like this
Apple
Google
MS
what should I do?
To add line breaks to a textarea, use the addition (+) operator and add the \r\n string at the place where you want to add a line break, e.g. 'line one' + '\r\n' + 'line two' . The combination of the \r and \n characters is used as a newline character.
The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.
Talking specifically about textareas in web forms, for all textareas, on all platforms, \r\n will work.
Try nl2br()
instead:
echo nl2br($str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With