Reputation: 23
I am writing a form using php the form has a textarea and the data is sent to the script using $_POST
; then stored into db.
The problem is that when the textbox is populated and if the user do not press enter to make a new line break but he wait until the end of the width of the textarea the text stored in the db is all in one line, for example if he wrotes:
HELLO THIS IS TEXT AND IT COVERS ALL THE WIDTH OF THE TE<=end of textbox (no br no /n) XTAREA.
This even if when you reach the maximum width of the textbox the text goes in a new line. when I print the message with a query the result is all in one line (which screw up my site layout)
I have managed to record user ENTER strokes by using this code:
$cleaned_message=str_replace(Chr(13),'<br>', $cleaned_message);
But i cannot figure out what to do if he doesn't use ENTER KEY.
Upvotes: 1
Views: 2308
Reputation: 201538
If the user does not use the Enter key (and does not otherwise insert line breaks, e.g. by copying and pasting text that has line breaks), then there are no real line breaks in the data, as stored in the DOM and as submitted in the form data. Browsers divide the text in several lines as needed to make it fit, but the line breaks are “soft,” just visual rendering.
The user-entered line breaks are transmitted as CR, LF pairs (Carriage Return, Linefeed). In the default form data encoding, this means %0D%0A
. What you do with them depends on the context. Line breaks might be retained and interpreted as paragraph breaks or as content-significant line breaks (e.g., in postal addresses, poems, and computer code), or they might be replaced by spaces.
There is no way and no need to deal with browser-generated line breaks server-side, since they don’t reach the server.
However, if the textarea
element has the (nonstandard) attribute wrap=hard
, then the browser-generated line breaks become real line breaks (and they are indistinguishable from user-entered line breaks). The attribute wrap=off
prevents automatic wrapping: a line can be arbitrarily long, and horizontal scrolling appears as needed.
Upvotes: 1
Reputation: 3748
What you describe in first part of your post is a normal and correct behavior. Text is wrapped in the textarea and as long as user does not hit enter, it's interpreted as one line. It is the user to specify where he wants to have a new line, not the textarea.
What's broken is most probably your layout. Try to define width of the output element (I guess this is also a textarea), so the output is wrapped in the element, rather than element adjusted to the output.
Could you update your post with a part of the layout where the result is printed?
Upvotes: 1