quick and easy question... please help

Discussion in 'PHP' started by jfontestad, Dec 7, 2008.

  1. #1
    i have a form where a user submits text.
    for example a text field and they submit:

    hello,

    my name is so and so.

    thank you.

    it's inserted into the db like that, but when its displayed it comes out as:

    hello, my name is so and so. thank you.

    all on the same line. how do i make it so that it keeps its format as it is in db.?

    would i have to have it insert <br> tags after each line? if so how would i do that?
     
    jfontestad, Dec 7, 2008 IP
  2. Spawny

    Spawny Well-Known Member

    Messages:
    252
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #2
    try using nl2br($string); to output your string
     
    Spawny, Dec 7, 2008 IP
  3. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #3
    on my submit form that inserts it into the db or should that go to my output form that actually shows the data?
     
    jfontestad, Dec 7, 2008 IP
  4. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #4
    also, there is no \n or anything in the record in the db.
    it's automatically input with spaces and line breaks in the database.

    just that when it goes to display it on the display page it's all jumbled up on one line.
     
    jfontestad, Dec 7, 2008 IP
  5. jfontestad

    jfontestad Well-Known Member

    Messages:
    1,236
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    148
    #5
    it works, but i need to to insert it into the db the <br> tag...
    if i use that function it adds a double space to all existing records that already have the br tag.
     
    jfontestad, Dec 7, 2008 IP
  6. mac83

    mac83 Active Member

    Messages:
    237
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Before inserting into databse, use nl2br function and then insert!

    So $_POST['fieldname'] will become nl2br($_POST['fieldname'])
     
    mac83, Dec 7, 2008 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    There is another way that would handle it client side... use white-space: pre; - just be warned that you lose wordwrap.
     
    deathshadow, Dec 7, 2008 IP
  8. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #8
    pre works perfect for me :)
     
    harrisunderwork, Dec 8, 2008 IP