Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

storing whitespace in db that comes from textarea

Participant ,
Apr 16, 2008 Apr 16, 2008
hi guys,

i am taking user input from a textarea in a form and saving that in my db.

as we all know when one does that the whitespace is sent from the browser to the server but from there on it can get 'lost'

in a previous application i built i think i did something like this -- replace(form.text_from_textares,chr(10),"<br />,"all") -- and then stuck that in the db

but to be fair i never really understood exactly what chr(10) represented and if that was infaliable (ie did it catch all 'white space'?)

anyway - i dont think this is ideal as if one then want to search for a string that is broken up with a carridge return or similar your search code would be unlikely to find it as you've gone and stuck a BR tag in the middle of it.

what is considered best practice here?

PS if anyone is able to explain the differences between CR and LF and what they're all about that would be great!

PPS what's with the "wrap" attribute in textarea - is that depreciated now?

PPPS would using the regex shortcut of \s be a way to tackle this issue?

thanks in advance

Nick
TOPICS
Getting started
480
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 16, 2008 Apr 16, 2008
A line feed brings your curser down one line. A carraige return brings it to the left of the current line. When you are typing, and you press the Enter Key, you get both a line feed and carraige return (I think).

The best practice is a matter of opinion. My preference is to store what the user gave entered and do any formatting as necessary. It won't necessarily always be html.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 16, 2008 Apr 16, 2008
hi Dan

thanks for this.

when you say "do any formatting as necessary" do you mean change chr(10) into BR when you're later on displaying the user's input that you stored? if so what do you look for and what do you replace it with?

also when you say "It won't necessarily always be html" - would you mind elaborating on that?

thanks ever so much

Nick
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2008 Apr 16, 2008
I think the HTMLCodeFormat() function will help you get what you need. It preserves line feed info and escapes characters like & and <, >.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 16, 2008 Apr 16, 2008
LATEST
happysailingdude wrote:
> hi guys,
>
> i am taking user input from a textarea in a form and saving that in my db.
>
> as we all know when one does that the whitespace is sent from the browser to
> the server but from there on it can get 'lost'
>

Actually, unless something unusual is going on in your system, the white
space is not 'lost'. Most likely it is right there in the database
where it is supposed to be.

The issue here, generally, is to understand the technologies being used
and how they function. The primary one here is the HTML standard that
says ALL white space (spaces, tabs, newlines, carriage returns, etc.) is
collapsed into a single space character.

So the whitespace is not getting lost, it is getting ignored according
to the standard specification of the technology being used. In fact if
one where to view the source in the browser one would see all the white
space that is being ignored by the browser as it is designed to do.

The first way around this is to use the features of the technology
designed to work with this. In HTML that is the <pre>...</pre> and
<code>...</code> tags. CFML adds functions such as htmlEditFormat() and
htmlCodeFormat() to also work with this type of data. Beyond this you
can role your own solution to display the received data in a desired format.

Dan's suggestion is that you do not do any transformations on the data
going into the database, because this locks you into a single format.
If one just stores the data as is in the database, and then do the
transformation to the desired format at display time, one can then
create different displays for different technologies and purposes from
the same data.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources