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
https://forums.phpfreaks.com/topic/167881-solved-simple-html-applied-to-output/
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>

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?

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.

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

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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.