voyde Posted March 10, 2008 Share Posted March 10, 2008 I need some help... If someone enters data into a form a textbox, with multiple lines how can I get PHP to convert each new line into a <br /> so when it saves to the database it automatically has the proper spacing? Quote Link to comment https://forums.phpfreaks.com/topic/95491-form-with-proper-spacing/ Share on other sites More sharing options...
Psycho Posted March 10, 2008 Share Posted March 10, 2008 I'm assuming you meant to post a < br /> tag above. I would suggest NOT converting line breaks to BR tags before saving to the database. Otherwise youwill have to convert again if you need to repopulate a textarea for editing. Instead, when you get the content from the database, use nl2br() when displaying it. Quote Link to comment https://forums.phpfreaks.com/topic/95491-form-with-proper-spacing/#findComment-488799 Share on other sites More sharing options...
voyde Posted March 11, 2008 Author Share Posted March 11, 2008 I dont seem to follow.... <textarea cols="40" rows="20" name="text"><?=$text?></textarea> do i convert it into a nl2br(String) before i send it to the DB? or when im trying to read the DB Quote Link to comment https://forums.phpfreaks.com/topic/95491-form-with-proper-spacing/#findComment-489183 Share on other sites More sharing options...
tippy_102 Posted March 11, 2008 Share Posted March 11, 2008 Use nl2br when you are reading from the database to retain the line breaks. Quote Link to comment https://forums.phpfreaks.com/topic/95491-form-with-proper-spacing/#findComment-489233 Share on other sites More sharing options...
voyde Posted March 11, 2008 Author Share Posted March 11, 2008 Ok well how in a form input do i retain the breaks? I.E on the way into the database. Quote Link to comment https://forums.phpfreaks.com/topic/95491-form-with-proper-spacing/#findComment-489248 Share on other sites More sharing options...
Psycho Posted March 11, 2008 Share Posted March 11, 2008 User enters this into a textarea: Here is some text with several line breaks You would enter that (as is) into the database. Now when you need to display that text in HTML you would use this: nl2br($textFromDB) which would output this: Here is<br />some text<br />with several<br />line breaks However if you need to populate the original text back into a textarea for editing you just use the value from the database w/o nl2br(). Quote Link to comment https://forums.phpfreaks.com/topic/95491-form-with-proper-spacing/#findComment-489556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.