whitestar73 Posted January 4, 2009 Share Posted January 4, 2009 Hi folks, I've got a curious situation with a test case I set up for myself. I created a simple form with a text area that holds content to be edited. Once edited, the data is loaded into mysql and then recalled back out to be placed into the text area for further editing. The problem I'm having is that whether I edit the text or not, it seems that a leading and trailing carriage return is added each time I hit the form submit button, so if you simply resubmit the form, say 5 times, you'll have 5 leading and trailing carriage returns in the text area. By all accounts, this simply shouldn't happen. I can't find any information anywhere that would indicate that it happens either by default or by some "setting". Googling will simply return pages where people want to ADD them rather than get rid of them even if I specify removal in the search engine, so I decided to join a forum and try to get a direct answer. So, hopefully, someone will know what's going on. I'd like NOT to have those phantom carriage returns showing up, and I don't want to have to create a stripping function, but if that's the only option, then that's that. I'm pretty new to web development, so go easy on me. All of my knowledge has been self taught. I've included setup code below to allow you to see what I'm seeing. Database setup: create a database called "test" with username and pwd of your choice OK, now, here's the code for the table creation CREATE TABLE `formtest1` ( `entry` int(10) unsigned NOT NULL auto_increment, `blog` text NOT NULL, PRIMARY KEY (`entry`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 Create a single entry in the table ahead of time. Put in any text you like. Next is the page code: (this can be placed in a single php file) <?php $user = "test"; $pwd = ""; $conn = mysql_connect('localhost', $user, $pwd) or die ('Cannot connect to server'); mysql_select_db('test') or die ('Cannot open database'); if ($_POST['blog_update']){ $blog_update = $_POST['blog_update']; $blog_update = mysql_real_escape_string($blog_update); $sql = "UPDATE formtest1 SET blog = '$blog_update' WHERE entry = 1"; $result = mysql_query($sql) or die(mysql_error()); $_POST['blog_update'] = ""; } ?> <?php $user = "test"; $pwd = ""; $conn = mysql_connect('localhost', $user, $pwd) or die ('Cannot connect to server'); mysql_select_db('test') or die ('Cannot open database'); $sql = "SELECT blog FROM formtest1 WHERE entry = 1"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { $result_array[] = $row; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>form test</title> </head> <body bgcolor="black" text="white"> <FORM Method="POST" Action="textarea3.php"> <P>Your comments?<BR> <TEXTAREA Name="blog_update" rows="15" cols="125"> <?php echo $result_array[0]['blog']; ?> </TEXTAREA> </p> <P><INPUT Type="submit" Value="Send your info"> <INPUT Type="reset" Value="Cancel and reset page"></p> </FORM> </body> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/139432-solved-mysterious-leading-and-trailing-carriage-returns/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2009 Share Posted January 4, 2009 It's because the HTML you are outputting contains carriage returns. <TEXTAREA Name="blog_update" rows="15" cols="125"> <?php echo $result_array[0]['blog']; ?> </TEXTAREA> You will also find that if you do it like this - <TEXTAREA Name="blog_update" rows="15" cols="125"> <?php echo $result_array[0]['blog']; ?> </TEXTAREA> That some browsers will still add carriage returns. So the proper way to get just your output is - <TEXTAREA Name="blog_update" rows="15" cols="125"><?php echo $result_array[0]['blog']; ?></TEXTAREA> Quote Link to comment https://forums.phpfreaks.com/topic/139432-solved-mysterious-leading-and-trailing-carriage-returns/#findComment-729360 Share on other sites More sharing options...
whitestar73 Posted January 4, 2009 Author Share Posted January 4, 2009 (smacks forehead) Well, I feel like an idiot. PFMaBiSmAd, that definitely solved it! I completely forgot that whitespace sorta doesn't apply when inside a textarea of html since it's interpreted literally (except the php code part). Well, that's just one more thing to keep in the back of my head when coding LOL! It's amazing the problem was just staring me right in the face the whole time, but I was just thinking in "whitespace" mode while looking at the code. Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/139432-solved-mysterious-leading-and-trailing-carriage-returns/#findComment-729377 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.