Jump to content

[SOLVED] Alter how page echo's data grabbed from txt file


DanielHardy

Recommended Posts

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

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.