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.
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?
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.
Related
I have a TEXTAREA on my form that saves to MySQL. Works 100%. Users can use most HTML expressions in it such as font size and color. When displaying the TEXTAREA variable in HTML I use REGEX to sift out all the tags like and it works 100% as well. The problem that I'm having is that when a user wants to do a line break (or carriage return or new paragraph, etc), MYSQL shows it as such:
$str="stuff
more stuff";//this is how I see it in MYSQL
but when displayed it is:
echo $str;//output is "stuff more stuff"
Any suggestions? I was thinking of listening for the user to hit ENTER and then adding before saving to the MYSQL but I have no idea how to do this.
I am new to php mysql.
I have a textarea and a button. When the user clicks the button, the value in the textarea is stored in the mysql database. The problem i am facing is...
If the user enters text like
hey this is my text (line break)
and this is my second line text
...the database stores it like:
hey this is my text and this is my
second line text
where (linkbreak) which is actually line break by pressing enter key. I add
pre tag before saving it in database it works fine but all of its css formating like bold font size and font family gets lost..
how can i solve this problem..
When you store text in a database, think of it as a string of individual characters with no presentational meta data attached (as in, what you see in the textarea that is submitted is not necessarily contained in the string that is posted).
The ability to see it in any format depends on context. The \n will be there, it just needs to be converted into <br /> to be seen on a web page (or in pre or element with white-space: pre).
Also, any colors or other presentational things will be dependent on the CSS used when outputting the string again.
You could also use nl2br($stringfromwebsite) to change all newlines to <br />'s
If you print out the text in PRE and it is displayed with line breaks, then the line breaks are retained. It’s just that HTML doesn’t regard about them in the normal text flow:
Line breaks are also white space characters. Note that although
and
are defined in [ISO10646] to unambiguously separate lines and paragraphs, respectively, these do not constitute line breaks in HTML, nor does this specification include them in the more general category of white space characters.
[…]
For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.
That’s the reason why HTML introduced the BR elements that represent visual line breaks. You can use nl2br that inserts BR elements in front of each line break character sequence.
I've bumped into a small problem that i cant seem to find the fix for.
I've got this simple form with a textarea, no biggie.
The problem shows when i edit a post i've already got in the database.
So, i retrieve the varchar field i have in my database and inserts it into my form by default.
If this is what i got by default:
this is some default text
with a line break
now, if i just saves this without doing any changes, this is what i get in the PHP $_POST variable:
this is some default text
with a line break
the /n character seem to multiply by itself. The next time i save the text the /n multiply again, giving me 4 /n in a row and so on.
It doesn't make any sense to me.
Step 1. I pre-fill the textarea with the contents mentioned in the first gray box above
Step 2. I submit the same content
Step 3. I run the following code before i do anything else:
echo '<pre>'. $_POST['content'] .'</pre>'; die;
Step 4. The newline are multiplied as in the second gray box above
That's easy.
there is some code in your application, that doubles line breaks. Just find it and get rid of it. Not a big deal.
(If you don't trust me, as you obviously do, create a simple testing script consists of just form and nothing else and see)
Wild guess: Windows CR LF line breaks.
In Windows, if you type a line break, they consist of not only \n, but \r\n. If MySQL implicitly converts \r to \n when inserting/updating, this would explain this behaviour.
Try removing any occurences of \r in the PHP first and see if that helps.
Remove line break using trim
HI,
I am creating on comments form where users will be commented and will be stored in the MYSQL database. The problem what I am facing is, it is stored as the single line in the database. It should be stored with exact format how user is entered in the form(like new lines and everything). I am using PHP to store it in the MySQL db.
First store it as text or longtext. Second, when showing the comment, use a function like nl2br to convert newlines to html <br> elements. This way, linebreaks are preserved.
Your text is stored just fine in the database if you are putting it into a long enough text-type field (e.g. TEXT), including the newlines in the user input.
Your problem is how to display the text formatted the way the user was seeing it when entering it. This is a more generic problem, and it only has to do with how HTML treats whitespace.
One approach would be to call nl2br on the comments, as Ikke says. This would replace all newlines (which the browser disregards) with <br> tags which have a visible effect on the rendered output.
Another option would be to put the text inside a <pre>...</pre> tag. This will force the browser to render it with whitespace preserved.
It's really up to what's more convenient/suitable for you.
Update: Just to be clear: do not modify the user input before inserting it in the database (unless it's part of your input validation, like e.g. stripping HTML tags from the input). Store it in an "untouched" format, and only do some processing on it before you output the data. This way, you always have the option of performing the correct processing if your output channel changes in the future (e.g. export comments to a text file vs displaying them as HTML).
you can store the comments in the same form in the mysql database. one difference would be when you retrieve the comments that has new line your code should look for \r\n and interpret it.. and also when you insert the data in mysql you will have to escape ' and \ characters from the comment.
I have a website in which a user enters text into an html form. The nature of the content is such that it is likely the user will want to write multiple paragraphs. As it stands now, the form sends the text via POST to a PHP file which inserts the text into a database. On another page, the text is pulled from the database and displayed. When it is displayed, all user formatting is gone. Multiple spaces and line breaks are deleted. How can I save the formatting of the user? Instructing him to use HTML tags to format is not feasible for a couple of reasons. I have also considered the
<pre>
tag, but that creates layout-breaking long lines of text and changes the font.
Any help is very much appreciated.
I'm assuming you're dealing with just a bunch of plain text entered into a textarea, not some fancy HTML editor as the other answerer assumed.
The reason your line breaks are lost is that HTML doesn't treat line breaks as line breaks. Line breaks are treated as just another space. Only <br> is treated as a line break. (Or <br /> in XHTML) If this is what's happening, you can use the nl2br() function to convert all line breaks into <br>.
Multiple spaces are more difficult. HTML doesn't distinguish between one space and many spaces. Spaces, tabs, line breaks, or any combination thereof, it doesn't matter, it's all treated as a single space. One way to prevent this is to wrap the whole thing in a <pre> or <code> block. But this is ugly unless you're trying to display computer code.
Or if you really desparately need those extra spaces, you could replace all spaces with which forces web browsers to display an extra space. (See edit below.)
Edit: Definitive version which preserves both line breaks and multiple spaces, and also prevents XSS:
<?php echo nl2br(str_replace(' ', ' ', htmlspecialchars($text))); ?>
You could use a rich text editor (TinyMCE, CKEditor) on the initial form which will allow the user to create markup without needing to know how to write HTML.
You then save the submitted markup into your DB (optionally filtering it for unwanted markup / scripting).
When displaying, don't use htmlspecialchars / htmlentities as you will want the content to be interpreted as HTML.