I have a form with a textarea whose results are inserted into a mysql database. I'm using nl2br to preserve the line breaks.
However, because this inserts br's in the text, when a user goes to edit what they've entered in the textarea, it shows all the br's in the textarea which were saved in mysql (looks ugly for people who don't know html).
So, if I don't use nl2br, the line breaks look nice when echoed back in the textarea but not saved in the database correctly. If I use nl2br, the line breaks are saved in the database correctly but look ugly when echoed back. How do I echo the saved textarea contents back onto the page without showing the br's all over the place?
Store the test in the database with break lines and when you echo it out to the screen run it through nl2br.
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.
All,
I'm inserting some text from a textarea in a mySQL database. I'm using mysql_real_escape_string() to do this. I'm using stripslashes() to remove the "\" that gets inserted in front of this to display it. However when I output this it gives me the following result for something like wasn't I get wasn\\'t and if someone hits enter in my text area I get \r\n\r\n and that is also displayed.
In my database it stores correctly with a single slash and the return but it doesn't output that the same way when it's outputed. Any idea why it would be doing this?
Thanks
As long as you are double checking how your outputting data. I used.. serialize(), and unserialize(). Takes care of any special characters it seems. Have you tried that?
I have a form with a textarea whose results are inserted into a mysql database. I'm using nl2br to preserve the line breaks.
However, because this inserts br's in the text, when a user goes to edit what they've entered in the textarea, it shows all the br's in the textarea which were saved in mysql (looks ugly for people who don't know html).
So, if I don't use nl2br, the line breaks look nice when echoed back in the textarea but not saved in the database correctly. If I use nl2br, the line breaks are saved in the database correctly but look ugly when echoed back. How do I echo the saved textarea contents back onto the page without showing the br's all over the place?
Store the test in the database with break lines and when you echo it out to the screen run it through nl2br.
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.
Quick and simple (I hope) question; If a user inputs new lines in a text field, ie:
43 Dennis
Beeston
How can I save the new line rather than have it transferred to the mysql server as one line?!
I am using PHP and mysql.
When you output the field from the database to a html document, either use <pre> or nl2br() (or a <textarea>).
To save the newlines in the database, take a look at mysql_real_escape_string
If you want your newlines displayed as breaks on the page when you display the records, use nl2br.
MySQL should preserve the newline - could it be your output (or your mysql gui) that isn't displaying it?