I have a form with a text area as one of the input fields. I want to store the user entered data. In the process I need to store the \n (enter key). But here the problem is in the DataBase (MySql) data is stored, but \n is not stored; i.e. data store as:
.
When I display the same data in the browser it shows in single line.
I need to split the data with \n and it should look like:
When I display the same data in the browser it shows in single line.
Yes, it would - because a newline in HTML doesn't get rendered as a newline. If you look at the raw HTML, I suspect you'll still see the line breaks... it's just that's not how it's rendered.
In order to represent line breaks in HTML, you need to use <br /> between lines, or something similar - or display it with a <pre> tag.
Basically, you need to format your raw text as HTML, however you decide to do that. If you're just dumping the HTML straight into your page, you may well find you already have issues if the text contains HTML tags - they should be escaped.
If you are seeing \n being stored in database, then you can write logic to include
<br/> tag while printing database values on your jsp.
Related
I'm populating a multi-line TextBox from MySQL, and the data is basically paragraphs with multiple lines with line breaks in between. My data looks fine in the database; when I copy and paste the data into a text editor (I use NotePad++) directly from workbench, the data looks fine.
There's a curious space between Test2 and Test3, but I can live with that.
When I try and MsgBox it, it looks fine too:
But when it comes to adding it to my TextBox, my problem comes:
I've looked at several resources, including here, but I couldn't find a solution that worked, nor a reasonable explanation why this is happening. Does anyone at least have an explanation so I can figure out a workaround?
It appears, from the text pasted into Notepad++, that the original string copied from MySql Workbench, contains multiple line feed chars and, probably, a space which separates the text parts.
This becomes more evident, since a MessageBox shows the text separated in multiple lines.
The .Net MessageBox wraps the User32 MessageBox function, which allows \n (VbLf - CharW(10)), \r (VbCr - CharW(13)) or both (VBCrLf) as line separator(s).
The string parameter of the message part, lpText, will parse and adapt any of these:
The message to be displayed. If the string consists of more than one
line, you can separate the lines using a carriage return and/or
linefeed character between each line.
A standard TextBox instead requires that a LineFeed+Carriage Return (\r\n) are used to separate lines of text: when the text is pasted in a TextBox control, since only \n is used, the text is not separated in different lines.
The RichTextBox control is more international (MacOs also ditched \r for the *nix \n): it uses \n to separate lines of text.
If we provide \r\n (Windows style) as line separator, the control converts \r\n to just \n.
Thus, pasting this text in a RichTextBox, the original line feeds are parsed as intended.
I'm sure there must be a lot of posts answering my query, but I am just not able to find the correct post.
In my application user is entering free from description in the text area. but when data is saved and displayed on the next page, entire text is wrapped in to single para. I want whole text to be saved as user enters it along with line spaces and newlines, tabs etc.
please redirect me to correct post. Do i have to save textarea as blob?
Thanks
Your problem is that the text is entered in plain text, however, in HTML, extra spaces are removed, and all sorts of space are just displayed as a single space character. You have to either display the text in a <pre></pre> tag (ugly way) or reformat it using regular expression or other string processing methods to make an actual HTML.
I have inserted my code in mysql database using text area.
What I have save appears is like this in mysql
This is Line 1
test
This lis Line 3
Now, my problem is to display the saved "file" to my browser which I am expecting to appear like this.
This is Line 1
test
This lis Line 3
Has anyone have some situation like this?
Use htmlentities on your output display. You can save html or any code as is in mysql with no special attention. You will need to escape it though so user based input isn't malicious.
http://www.php.net/manual/en/function.htmlentities.php
htmlentities("URL", ENT_QUOTES, 'UTF-8');
If you run this in php you will display the whole html tag. Likewise, you can spew out results from a mysql query, wrapping the relevant content in htmlentities to achieve what you're looking to do.
I have what is a simple problem that hopefully has a simple solution:
I have a site written in PHP and HTML, using a Linux server with MySQL.
It has a form where users fill in some personal info, including a textarea in which
they are meant to copy and paste a test CV.
I have also set up a back end for my client where she can query the database to see who
registered and retrieve their info.
My problem is that when I query and echo the content of the table row that contains the
CV (alot of text), the line breaks are all gone - everything is printed in one line.
Does someone know if I can solve this by using the right kind of collation/character encoding
for that specific row that contains the users's cvs? I am hoping that such collation exists that saves and maintains line breaks.
Collation has nothing to do with it - collations and charsets won't touch your newlines at all. If you want to see it, look at the page source of the echo'd text.
HTML, however, treats line breaks like all other whitespace under normal circumstances, so they won't be visible when you echo them to a browser. You shouldn't be outputting plain text as HTML anyway, because they're not the same. You must convert the plain text to HTML first; a simple method is to call htmlspecialchars() and nl2br() on the text (in that order, otherwise htmlspecialchars will eat your newly-created br tags and turn them into <br/>. Failing to do so will not only create undesired output, it can also be a major security risk (XSS).
Use nl2br($text) to add HTML line breaks.
I don't think collation is related to this. Break lines from the textarea come in the form of the \n or \r characters. If you are not doing anything "weird" those break lines should be stored into the DB.
I think your problem is when you echo the content of the table, since the browser doesn't display the \n and \r as new lines, you have to either substitute them for <br/> element or wrap each paragraph in a <p></p>
You can use nl2br() for that.
or how about wrapping the text in a <pre> </pre>
see: http://www.w3schools.com/tags/tag_pre.asp
I have a text area in html and a form to store the information in the database. When I click on submit, it supposed to take this text and save it in the database. For example:
"This is a text and this is a list:
1. number 1
2. number 2"
However, when I load the information from the database it looks like:
"This is a text and this is a list: 1. number 1 2. number 2"
How do I keep the layout of the textarea not changed (keep the spaces, lists, etc) without the need for the user to enter any tags.
It's being stored just fine in the database. You're outputting what was entered as plain text as HTML, and HTML ignores line breaks. You need to convert your \n characters to <br /> tags. PHP has a nl2br() function for this.
You could have them enter the data in a WSYWIG and do the work on your side to make sure it's always formatted properly - client-side users still won't have to see any tags, especially if you limit their editing options...
tinyMCE, nicEdit are two good editors