DanielHardy Posted July 29, 2009 Share Posted July 29, 2009 Hi all, First post from me for a while. I am working on a simple guestbook that works with a txt file rather than a database (my client did not want to fork out for the database). I have the system up and running and simply want to be able to display the results better. Here is my code <?php $guestbook = "messages.txt"; if (isset($_POST['button'])) { if (!empty($_POST['name']) && !empty($_POST['message'])) { $string .= "<b>" . $_POST['name'] ; $string .= str_replace($badSearch, $goodSearch, $_POST['email']) . "</b>\n"; $string .= $_POST['message'] . "\n<hr>"; $file = fopen($guestbook, "a"); fwrite($file, nl2br(strip_tags($string, "<b><hr>"))); fclose($file); } else { echo '<script>alert("Please fill out the whole form")</script>'; } } $readfile = fopen($guestbook, "r"); echo @fread($readfile, filesize($guestbook)); fclose($readfile); ?> I want the title to be bold and blue and the message to be black. I have tried the obvious <font color="#....."> within the statement that stores the data in the txt file. it simply will not work. I gather this is a simple fix but as I am still somewhat of a novice I greatly appreciate your help. Thanks Dan Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 29, 2009 Share Posted July 29, 2009 any time you are changing fonts you want to use css and not <font> tags. just use something like this //put this in your header <style type="text/css"> .title { color:blue;font-weight:bold; } .message {color:black; } </style> //put this wherever you output the data <span class="title"> title</span> <span class="message">message</span> Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 Thanks.I know about using <font> due to w3c standards and what not, i just want to use it for the time being. Problem is the code (where I am placing it) will not accept html tags. Similar to where I have placed the <B> tag for the name string, I am trying to enter the <font> tag in exactly the same place for the message string. However it won't work. I can only presume I am placing it in the wrong place. Ideas Please? Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2009 Share Posted July 29, 2009 Have you looked in the text file, does the tags go in the file as they should? Also have you declared $string variable anywhere before you start concencating the string after $_POST['button']? Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 29, 2009 Share Posted July 29, 2009 when you view the page source what does the output look like ? it seems you are running strip_tags before writing to the file. This will remove any type of formatting that you use Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 the <b> tag goes in fine yes. The <font> tag doesn't and the code dies also Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2009 Share Posted July 29, 2009 when you view the page source what does the output look like ? it seems you are running strip_tags before writing to the file. This will remove any type of formatting that you use strip_tags takes for second argument allowed tags, and he is allowing <b> and <hr> tags. Edit: if you want also font tags you must allow them in strip_tags function like you do for <b> and <hr>. Otherwise this function removes them. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 29, 2009 Share Posted July 29, 2009 strip_tags takes for second argument allowed tags, and he is allowing <b> and <hr> tags. thanks, didn't read that far in the manual Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 <?php $guestbook = "messages.txt"; if (isset($_POST['button'])) { if (!empty($_POST['name']) && !empty($_POST['message'])) { $string .= "<b>" . $_POST['name'] ; $string .= "<font color="#2766c5">" . $_POST['message'] . "\n<hr>"; $file = fopen($guestbook, "a"); fwrite($file, nl2br(strip_tags($string, "<b><hr><font>"))); fclose($file); } else { echo '<script>alert("Please fill out the whole form")</script>'; } } $readfile = fopen($guestbook, "r"); echo @fread($readfile, filesize($guestbook)); fclose($readfile); ?> New updated code. Still having problems. point to it please? Thanks Dan Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2009 Share Posted July 29, 2009 Have you checked inside the textfile that the data really goes there as you think it should go? Also do you have errors on (you can put this code in the beginning of your file to see all possible errors: error_reporting(E_ALL)? Also it is good idea to remove the '@' in front of actions like fread during development because this will prevent you seeing any errors happening with fread in this case. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 29, 2009 Share Posted July 29, 2009 i think the problem is with your quotes here $string .= "<font color="#2766c5">" . $_POST['message'] . "\n<hr>"; to $string .= "<font color=#2766c5>" . $_POST['message'] . "</font>\n<hr>"; and also you aren't closing any of your HTML tags, which can cause problems with strip_tags. make sure your <b> and <font> tags are all properly closed Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 as always something very simple causing big problems! Problem solved. Thanks very much! Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted July 29, 2009 Share Posted July 29, 2009 dont forget to mark the topic solved Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 don't worry i'll give credit where it's due Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 29, 2009 Share Posted July 29, 2009 Indeed exactly what lonewolf217 said. Not sure but I think it would be still good to surround the HTML tag attributes inside quotes which you could do by escaping them or just using single quotes instead while assigning php variable. with escaping: $string .= "<font color=\"#2766c5\">" . $_POST['message'] . "\n<hr>"; with single quotes: $string .= '<font color="#2766c5">' . $_POST['message'] . '\n<hr>'; 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.