dbillings Posted July 16, 2007 Share Posted July 16, 2007 I have a problem understanding how I can submit form data to a mysql database and preserve the formatting that the user wants. Essentially the problem is line breaks. When a user inserts data into a text area the text automatically jumps to the next line when the one line fills up with text. Yet when the data is submitted to mysql it doesn't preserve the breaks. I've tried several solutions like the <pre> tag. Basically I want the break that the html textarea creates automatically when it reaches the end of the text area to generate a <br /> tag in my data submission. If anyone can give me a solution I would appreciate it. Thanks. Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 16, 2007 Share Posted July 16, 2007 Use nl2br() on the output from the database - http://ca.php.net/manual/en/function.nl2br.php Quote Link to comment Share on other sites More sharing options...
trq Posted July 16, 2007 Share Posted July 16, 2007 asically I want the break that the html textarea creates automatically when it reaches the end of the text area There is no linebreak created automatically, its just the wrapping affect of displaying data within a textarea. You could try using wordwrap when displaying this data back to fake it. Another function to look into would be nl2br, this comes in handy for changing actual linebreaks into <br /> tags. Note: Both these functions should only be applied to your data on the wway out of the database. Store your data in its raw format wherever possible. Quote Link to comment Share on other sites More sharing options...
dbillings Posted July 16, 2007 Author Share Posted July 16, 2007 <pre>Hello This is a test of how this forum handles a pre tag </pre> Quote Link to comment Share on other sites More sharing options...
trq Posted July 16, 2007 Share Posted July 16, 2007 Just place the data with a fixed sized table or better still a div, this really is a html issue, nothing to do with php. Quote Link to comment Share on other sites More sharing options...
dbillings Posted July 16, 2007 Author Share Posted July 16, 2007 This is a test to see if this forum preserves line breaks without using newline or break code. Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 dbillings, basically you have 2 options (yes it always comes down to 2 options it seems) 1. Use a WYSIWYG type Editor and save the contents of that to your DB 2. Do as suggested, save the textarea as normal, and then when you do something like this: <?php $q = "select textarea from myTable where id='1'"; $result = mysql_query($q); $data = mysql_fetch_array($result); print nl2br($data['textarea']); ?> Quote Link to comment Share on other sites More sharing options...
dbillings Posted July 16, 2007 Author Share Posted July 16, 2007 See my above post isn't formated with a div or table. Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 It most certainly used nl2br http://php.net/nl2br or a variation. Here's your post's source: <div class="post"> This is a test<br />to see if this forum <br /><br /> .. etc Quote Link to comment Share on other sites More sharing options...
dbillings Posted July 16, 2007 Author Share Posted July 16, 2007 nl2br doesn't produce the non breaking spaces in the beginning. And a pre html tag will give you a run on sentence unless the user hits return. grrrr. any suggestions for a wysiwyg editor? Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 TinyMCE. To get the non breaking spaces, feel free to write your own function, something like.. <?php function format_output($out) { /* replaces " " with %nbsp (replace with &) */ return nl2br(str_replace (" ", "%nbsp; ", $out)); } ?> Quote Link to comment 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.