DanielHardy Posted August 13, 2009 Share Posted August 13, 2009 Hi all, Please see my code below. <form id="form1" name="form1" method="post" action="<?= $PHP_SELF ?>"> <label> <div align="center"> <table width="100" border="0"> <tr> <td height="20"><p> <font size="2px" ><strong><center>Name</strong><br /></font> <input type="text" name="name" id="name" style="border: 1px solid #2766c5;"/> <br /> <br /> <p><font size="2px" ><strong>Message</strong><br /></font> <textarea wrap="hard" name="message" id="message" cols="16" rows="6" style="border:1px solid #2766c5;"></textarea> </p> <p> <input type="image" src="http://www.chcsc.org/images/submit.jpg" name="button" id="button" value="Submit" STYLE="background-color:#2766c5;" /> <input type="image" src="http://www.chcsc.org/images/reset.jpg" name="button2" id="button2" value="Reset" /> </p></td> </tr> </table> </div></label> </form> <div id ="cheese" style="margin-top:70px;" align="center"> <table width="200" border="0"> <tr> <td width="200"> <?php // Save filename to variable. $guestbook = "messages.txt"; // This will run if someone sends user input. if (isset($_POST['button_x'])) { // Check whether any fields are empty or not. if (!empty($_POST['name']) && !empty($_POST['message'])) { // Bad characters in E-Mail string. $badSearch = array("@", "."); // Goods characters in E-Mail string. $goodSearch = array(" @ ", " . "); // Grab name from the form and add a newline afterwards. $string .= "<b><center>" . $_POST['name'] . " "; // Grab email and replace the @ and . in the string. $string .= str_replace($badSearch, $goodSearch, $_POST['email']) . "</center>\n"; // Grab the message and save it in $string. $string .= "<font color=#2766c5>" . $_POST['message'] . "</font>\n<hr>"; // Open messages.txt and append more posts to it. $file = fopen($guestbook, "a"); // Write $string to the text file and replace bad characters to avoid XSS attacks. fwrite($file, nl2br(strip_tags($string, "<b><hr><font><center>"))); // Close the file we opened. fclose($file); echo '<script>alert("Your comment has been added ")</script>'; } else { // Of the form fields are empty will there popup a message. echo '<script>alert("Please fill out the whole form")</script>'; } } $readfile = fopen($guestbook, "r"); echo @fread($readfile, filesize($guestbook)); fclose($readfile); ?> </td> </tr> </table> This Code produces the guestbook part of the page at www.chcsc.org As you can see, the message bit of the entries is being displayed in exactly the same way as it is being entered into the form. Is there a way to keep the breaks in the text area when the user is entering input, but stop it from inserting these breaks into the txt file? Any help is greatly appreciated. Thanks Dan Quote Link to comment Share on other sites More sharing options...
.josh Posted August 13, 2009 Share Posted August 13, 2009 $string = str_replace("\n","",$string); Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted August 13, 2009 Author Share Posted August 13, 2009 please expand? Still a relative novice Quote Link to comment Share on other sites More sharing options...
nbarone Posted August 13, 2009 Share Posted August 13, 2009 \n is a "newline" characters, as in a line break or return. that line replaces "\n" with "" (nothing), removing your issue. Quote Link to comment Share on other sites More sharing options...
trq Posted August 13, 2009 Share Posted August 13, 2009 You should use nl2br when retrieving your data from your text files, not when writing to it. 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.