DanyalSakrani Posted March 9, 2007 Share Posted March 9, 2007 Hello All , I need help with one of my scripts (Reply.php for my Forums): Everytime a user submits a post with a break line, this shows up, "rn". An example: // The user enters his post in a textbox: "Hello, my name is Danyal. I like cookies." When I fetch the Post, this shows up: "Hello, my name is Danyal.rnI like cookies." Here is my INSERT code: $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post=htmlspecialchars($Post); $Post= nl2br($Post); $Time = date("F j, Y, g:i a"); $query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error()); $query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array( $query ); $Today=date("U"); $UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1"; mysql_query($UUT) or die(mysql_error()); $nFID = $row["TID"]; $pstamp = date("U")-0; $query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error()); print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic."; print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>"; The nl2br doesn't work. Can anyone please fix the code for me? Thank you, Danyal Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 '<' (less than) becomes '<' '>' (greater than) becomes '>' is this what goes in the database you do no that nl2br only work's once the enter button is pressed dont you? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 Try this: <?php $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post=htmlspecialchars(nl2br($Post)); $Time = date("F j, Y, g:i a"); $query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error()); $query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array( $query ); $Today=date("U"); $UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1"; mysql_query($UUT) or die(mysql_error()); $nFID = $row["TID"]; $pstamp = date("U")-0; $query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error()); print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic."; print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>"; ?> --FrosT Quote Link to comment Share on other sites More sharing options...
DanyalSakrani Posted March 9, 2007 Author Share Posted March 9, 2007 Try this: <?php $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post=htmlspecialchars(nl2br($Post)); $Time = date("F j, Y, g:i a"); $query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error()); $query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array( $query ); $Today=date("U"); $UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1"; mysql_query($UUT) or die(mysql_error()); $nFID = $row["TID"]; $pstamp = date("U")-0; $query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error()); print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic."; print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>"; ?> --FrosT Hey, Nope . I get this: TestrnrnrnTest Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 try it in plain format please cheers remember to press the enter button. <?php $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post=nl2br($Post); $Time = date("F j, Y, g:i a"); $query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error()); $query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array( $query ); $Today=date("U"); $UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1"; mysql_query($UUT) or die(mysql_error()); $nFID = $row["TID"]; $pstamp = date("U")-0; $query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error()); print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic."; print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>"; ?> Quote Link to comment Share on other sites More sharing options...
DanyalSakrani Posted March 9, 2007 Author Share Posted March 9, 2007 Hello, It didn't work, I get this: TestrnrnrnrnTestrnrnrn Quote Link to comment Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 How about this? <?php $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post = ereg_replace("\r\n", "<br />", $Post); ?> --FrosT Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 try this please <?php $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post=mysql_real_esape_string(nl2br($Post)); $Time = date("F j, Y, g:i a"); $query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error()); $query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array( $query ); $Today=date("U"); $UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1"; mysql_query($UUT) or die(mysql_error()); $nFID = $row["TID"]; $pstamp = date("U")-0; $query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error()); print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic."; print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>"; ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 frost he wants <br /> with enter button your code wont help will it? Quote Link to comment Share on other sites More sharing options...
DanyalSakrani Posted March 9, 2007 Author Share Posted March 9, 2007 Argh, no . TestrnrnTest - For Frost110 And redarrow, it shows me this: call to undefined function: mysql_real_esape_string() Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 <?php $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post=mysql_real_escape_string(nl2br($Post)); $Time = date("F j, Y, g:i a"); $query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error()); $query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array( $query ); $Today=date("U"); $UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1"; mysql_query($UUT) or die(mysql_error()); $nFID = $row["TID"]; $pstamp = date("U")-0; $query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error()); print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic."; print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>"; ?> sorry edited Quote Link to comment Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 I am not sure, it is worth a shot? How I see it is for some reason the nl2br is throwing off because it just does a newline, where as the post is returning a carriage return (\r) with a new line (\n) So in theory, converting \r\n to <br/> should produce the desired results...in theory. --FrosT Quote Link to comment Share on other sites More sharing options...
DanyalSakrani Posted March 9, 2007 Author Share Posted March 9, 2007 Didn't work . TestrnrnrnTest Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 if that true then str_replace would be better but that does not solve the problam it's a get out clause lol? Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 let's see this wonderful form? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 =) Yea, str_replace would of probably been better. Oh well it didn't work. I am not sure what is going on can you post the html code of the form you are using to post the data with? --FrosT Quote Link to comment Share on other sites More sharing options...
DanyalSakrani Posted March 9, 2007 Author Share Posted March 9, 2007 <? include('connect.php'); if(isset($_SESSION['user'])) { $Current=date("U"); if($getuser3[LPostTime] > $Current-10) { print "You Can Only Post Once Every 10 Seconds!"; } else { if(isset($_POST['Save'])) { if($type == "Locked" or $type == "PinLocked") { print "This Thread Is Locked!"; } else { $TID = $_GET['TID']; if($TID == "") { print "You Did Not Choose Which Thread To Post In!"; } else { $query = mysql_query("SELECT * FROM Threads where ID = '$TID'") or die(mysql_error()); $row = mysql_fetch_array( $query ); $TID = $row["ID"]; $FID = $row["FID"]; $type = $row["Type"]; if($TID == "") { print "The Thread You Chose To Post In No Longer Exists!"; } else { $Post = $_POST['Message']; if($Post == "") { $Post = "[No Message]"; } $Post = str_replace('\r\n', '<br />', $Post); $Time = date("F j, Y, g:i a"); $query = mysql_query("Insert into Posts (Post,Poster,Time,TID,FID) values ('$Post','$getuser3[username]','$Time','$TID','$FID')") or die(mysql_error()); $query = mysql_query("SELECT * FROM Posts where Poster = '$getuser3[username]' order by ID desc limit 1") or die(mysql_error()); $row = mysql_fetch_array( $query ); $Today=date("U"); $UUT="update trainers set LPostTime='$Today' where Username='$getuser3[username]' LIMIT 1"; mysql_query($UUT) or die(mysql_error()); $nFID = $row["TID"]; $pstamp = date("U")-0; $query = mysql_query("update Threads set OS = '$pstamp' where ID = '$TID'") or die(mysql_error()); print "Reply Posted Sucessfully. You Are Now Being Redirected To The Topic."; print "<meta http-equiv=refresh content='2; URL=index.php?function=ViewThread&TID=$nFID'>"; } } } } if(!isset($_POST['Save'])) { $Quote = $_GET['Quote']; if($Quote != "") { $query = mysql_query("SELECT * FROM Posts where ID = '$Quote'") or die(mysql_error()); $row = mysql_fetch_array( $query ); $post1 = $row["Post"]; $poster1 = $row["Poster"]; $time1 = $row["Time"]; $show_quote = " [quote]".$post1."[/quote]"; } print "<table width='50%'> <tr> <td class='top' width='50%'><center>Reply</center></td></tr></table> <table width='50%' border='0' class='top'> <form method='POST'> <tr> <td class='top'>Message:</td> <td class='top'> <textarea COLS='50' ROWS='5' accept-charset='iso-8859-1' name='Message' style='font: 10pt'Verdana'>$show_quote</textarea> <br><center><button type='Submit' name='Save' value='Save' class='SearchB'>Post Reply</button></center></form> </td></tr> </table>"; } } } if(!isset($_SESSION['user'])) { print "<font face='verdana' size='1'>You Must Be You Must Be Logged In To Reply To A Topic!"; } ?> Here is my code. I tried using str_replace with single quotes, it didn't work. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 9, 2007 Share Posted March 9, 2007 str_replace with the single quotes usually doesn't for \n it needs to be double quotes. It may have to do with the accept-charset? I am not sure, I try not to think about the charsets =) --FrosT Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 <form method='POST' action=''> Quote Link to comment Share on other sites More sharing options...
DanyalSakrani Posted March 9, 2007 Author Share Posted March 9, 2007 It didn't work. When I edit the "rn" from my SQL, it works. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 accept-charset='iso-8859-1 <<<<<<<<<<, this is why try without it ok. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 9, 2007 Share Posted March 9, 2007 It didn't work. When I edit the "rn" from my SQL, it works. Of course it will as you have edited it. However there is something that is escaping the newlines characters when you submit the form. Create a new script and put this in it: <?php if(isset($_POST['msg'])) { $msg = $_POST['msg']; echo 'Raw data: <pre>' .htmlentities($msg) . "</pre>\n"; echo "\n<hr />\n"; echo "Processed data:<br />\n" . nl2br($msg); echo "\n<hr />\n"; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Message:<br /> <textarea name="msg" rows="10" cols="50"></textarea><br /> <input type="submit" name="submit" value="Post Message" /> </form> what does that produce? Quote Link to comment Share on other sites More sharing options...
DanyalSakrani Posted March 9, 2007 Author Share Posted March 9, 2007 Raw data: test test Processed data: test test It breaks the line. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 9, 2007 Share Posted March 9, 2007 that correct what you wanted we all hope now inplement that in the current code ok. like i said was your action='' and char-set Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted March 9, 2007 Share Posted March 9, 2007 OK so that works. There must be something in your code which escapes the newlines characters. Not sure what's up but I will have a look. @ redarrow like i said was your action='' and char-set No that was my code DanyalSakrani said worked. 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.