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.
Related
I have a problem related with linebreaks of an inported text, previously inserted in MySQL using the classic PHP - HTML form method.
The problem is the following. I want to load the text saved in MySQL databe into a "news" section in my web page. The thing is that my web page has a PC version and a mobil version, each one with different widths.
So, I don't want to insert linebreaks when I submit the text to MySQL (I mean the line-breaks the form will submit if the text line width excedes a hipotethical "cols" width, assuming I'd do a "physhical" or "hard" wrap; the manually inserted line-breaks to separate parragraphs I want to keep of course) because, as far as I know, you have to specify "cols", which is a parameter that will tell the width of the lines before doing the linebreaks.
This is not interesting, because the text fields on my "news" sections will have different widths, as I've told you, so importing text with linebreaks from MySQL won't adjust the the two different "news" sizes in my web.
What I need is to upload text to MySQL with no linebreaks, and then, let the two "news" sections in my web do the formatting of the text.
I though this would be easy, as I'm just parsing the saved text in MySQL databse into a <div> tag with a specified width. But the thing is that the text overflows the <div> container width every time.
This is what I'm using as the text input in the HTML form:
<textarea name="STORY" wrap="physical">EXAMPLE</textarea>
To inject the news in MySQL I use the typical PHP:
$var = "INSERT INTO exampleTable ('story') VALUE ($_POST['STORY']);
To load the saved text, I just echo the value of a variable that imports the text from the story field of MySQL database between div tags:
echo "<div>".$story."</div>";
As you can see, because I don't wan´t to use a "hard" wrap when I insert the text from the from in MySQL to avoid inserting line-breaks in the lines that otherwise would exced the "cols" width, I use a "phisycal" wrap, but I don't specify "cols", so I think that should prevent the form from inserting line-breaks other than the ones I do manually (presing "enter" key).
But the resulting text, when I echo it, will overflow my div width, as I've told you before.
Shouldn't the div width wrap the text inside of itself?
Should I delete the wrap="physhical" attribute from the form?
First, please note that you're insertion into the database is very likely to be insecure, and will probably result in SQL Injections.
To remove every linebreak, you can do things like that:
echo "<div>".str_replace("\n", "", $story)."</div>";
You could try handling this in MySQL itself using the TRIM() function:
INSERT INTO exampleTable ('story') VALUE TRIM(TRAILING '\n' FROM $_POST['STORY'])
You shouldn't insert line breaks yourself - CSS can do this for you. Here is a SO answer that appears to do everything you might want to do.
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.
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.
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.