Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Formatting text area?!?

    Hello.

    I am having a problem with all my forms and stuff.

    How do I make a text area in a form, similar to the one that you use to post messages on this forum. Like what I mean is, with the database that I have at the moment, if you put a line break or somethign in the text area of a form, it doesn't add it in the database.

    Example: This is how I put it into the text area:-

    Hello!

    It's James Here.

    bye

    But this is how it comes out.

    Hello! It'/s James Here. bye

    any help?

    James

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    The tabs and newlines are almost certainly in your database, (unless you run a script that removes them first)

    The issue is converting those back into HTML as opposed to '\t' '\r' & '\n' etc

    You do not say which database or lannguage you are using to access it but in PHP


    <?echo nl2br($row['from_database']);?>

    will convert \n or \r\n to <br />(actually it adds a <br /> to the end of a string the \n is kept intact)

    if that function did not exist I would be using
    <?str_replace("\n","\n<br />",$row['from_database']);?>

    and there are similar replace functions in most server-side languages.

    as for the '\' before your apostrophe , PHP provides stripslashes() though you can str_replace them as well if required.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    66
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i am using a MySQL database - is it the same?

  • #4
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    What language are you using to write your web pages? PHP? ASP?

    In ASP, I usually do this:

    When you drop the textarea value into the database, escape() it first:

    var myTextValue=escape(Request.Form("myTextArea"));

    this replaces all the line breaks with "%0D%0A", so when you get the text back out of the database, you just replace it:

    var myText = rs.Fields("textarea").value;
    while(myText .indexOf("%0D%0A")>-1){
    myText = myText .replace("%0D%0A", "<br>");
    }

    And then you can Response.Write(myText) and it's got line breaks in.

    Any use?

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Spudhead, FYI - a much easier method (for ASP):

    Code:
    Function VbCrLfToBreak(byVal str) ''''''''''''''''
         If IsNull(str) Then str = ""
         VbCrLfToBreak = Replace(str,vbCrLf,"<br />")
    End Function '''''''''''''''''''''''''''''''''''''
    
    Response.Write(VbCrLfToBreak(rs("Whatever")))
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I forgot to mention, line breaks are preserved when you put a textarea into a database, you just have to replace them with <br /> when you retrieve them.

    So escaping them when you put them in is just a waste of time.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    New Coder
    Join Date
    Aug 2004
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi.. I have this same problem with a textarea in a form I made... line breaks are output as \r\n and nl2br() on the string just returns the same string, it's not working like it should for some reason. I also tried doing $str = str_replace("\r\n","<br />",$str); and this, for some reason, is not working either. I tried doing it to the string variable both before insertion to the databese, and also after retrieval, and nothing works... I keep getting the \r\n

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you post exactly what you retrieve from the database right now, compared to the string you input in the textarea? The answer lies there!!! I guarantee it.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #9
    New Coder
    Join Date
    Aug 2004
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, before I do that, I just want to ask: what's better, to replace \r\n to <br /> before insertion to the database, or insert it with the \r\n and then replacing the characters when the string is going to be displayed?

  • #10
    Regular Coder
    Join Date
    Jun 2004
    Posts
    112
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The first option would be better because it would be converted less times.

  • #11
    New Coder
    Join Date
    Aug 2004
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help, I solved the problem. It was a stupid function that was escaping special characters from my strings, turning \r\n into \\r\\n, and thus making nl2br useless. Thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •