Jump to content

[SOLVED] simple html applied to output


DanielHardy

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.