Rather basic messageboard script - error occurring?

I’m trying to write a php script where it displays a text form and a submit button. The page then loads the contents of a text file.

When the user enters text into the form, and hit submit, I’d like it so that the text file has their entry added, and then the page reloads.

Something like this:
User types “Lorem ipsum” into the form, and presses submit.
The text file was empty, so it is updated to say “Lorem ipsum”
Another user, using the same form, types “KFC is tasty”, and presses submit.
The new data is added to the start of the text file, to make it now read “KFC is tasty
Lorem ipsum”

Here’s the code I have so far:

<?php
//builds the page
echo "<html>";
//makes a form for the user to input to
echo "<form action=\"action.php\" method=\"post\">";
	echo "<p>Entry: <input type=\"text\" name=\"input\" /></p>";
	echo "<p><input type=\"submit\" name=\"Shbmit\" /></p>";
echo "</form><br><br>";

//collapses the array (wtf? must read up on this) $_POST into a string.
$entry = implode("", $_POST);

//below is the read section of the script
$myFile = "text.txt";
	
//opens the file - reading
$fh = fopen($myFile, 'r');
$fileLength = filesize($myFile);

//gets the data from the text file, only if the file has contents
if( filesize($myFile)!=0)
{
	$theData = fread($fh, filesize($myFile));
}
else
{ //if the file is blank, so is this var, ye.
	$theData = "";
}
	
//closes the file - reading
fclose($fh);
	
//opens the file - writing
$fh = fopen($myFile, 'w');
	
//only attempts to write to the file if there's already an entry
if(   strlen( $entry ) != 0   )
{
	fwrite($fh, ( entry + "<br><br>" + $theData ) );
}
	
//closes the file - writing.
fclose($fh);
	
//outputs the data read from the file - or, nothing,
//if there wasn't anything in the file to begin with...
echo $theData;
	
//end of this file
?> 

The error lies in execution: whenever the page is first loaded, it displays the original contents of the text file. When submit is clicked, however, the text file is made blank.

What am I doing wrong?

Modified the code - part of the problem was in the fopen call that used ‘w’. Didn’t know that that would delete all the data in the file lol.

But now, although the original data isn’t destroyed each time the script is run, all input is turned to a single ‘0’ (zero).

Here’s the code:

[code]<?php
//builds the page
echo “”;
//makes a form for the user to input to
echo “<form action=“action.php” method=“post”>”;
echo “

Entry: <input type=“text” name=“input” />

”;
echo “

<input type=“submit” name=“Shbmit” />

”;
echo “

”;
//collapses the array (wtf? must read up on this) $_POST into a string.
$entry = implode("", $_POST);

//below is the read section of the script
$myFile = "text.txt";

//opens the file - reading
$fh = fopen($myFile, 'a+');

$fileLength = filesize($myFile);

//gets the data from the text file, only if the file has contents
if( filesize($myFile)!=0)
{
	$theData = fread($fh, filesize($myFile));
}
else
{ //if the file is blank, so is this var, ye.
	$theData = "";
}


//only attempts to write to the file if there's already an entry
if(   strlen( $entry ) != 0   )
{
	fwrite($fh, ( entry + "<br><br>" + $theData ) );
}
 
//closes the file - writing.
fclose($fh);

//outputs the data read from the file - or, nothing,
//if there wasn't anything in the file to begin with...
echo $theData;

//end of this file

?>[/code]

YEAH

Still having trouble, but here’s the updated script.

[code]<?php
//builds the page
echo “”;

    echo "<body bgcolor=\"Black\" link=\"#CC6666\" vlink=\"#CC6666\" alink=\"#CC6666\" > ";
	echo "<font color=\"White\">";
    echo "<h1> messageboard - still under construction, you know, to fix the problem where it doubles the file length every time it's reloaded...</h1>";
    
    //makes a form for the user to input to
echo "<form action=\"message_board.php\" method=\"post\">";
	echo "<p>Entry: <input type=\"text\" name=\"input\" /></p>";
	echo "<p><input type=\"submit\" name=\"Shbmit\" /></p>";
echo "</form><br><br>";
echo "</html>";
    
//collapses the array $_POST into a string.
$entry = "";
$entry = implode("", $_POST);
$entry = substr (  $entry , 0 , -12 );

//below is the read section of the script
$myFile = "text.txt";

//opens the file - reading
$fa = fopen($myFile, 'r+');



//gets the data from the text file
$theData = "";
$theData = fread($fa, filesize($myFile));


//only attempts to write to the file if there's already an entry
if(   strlen( $entry ) != 0   )
{
	fwrite($fa, $entry );
	fwrite($fa, "<br><br>" );
	fwrite($fa, $theData );
}


//closes the file - writing.
fclose($fa);

//outputs the data read from the file - or, nothing,
//if there wasn't anything in the file to begin with..
    echo $entry;
    echo "<br><br>";
	echo $theData;
//resets the data 
unset($theData);
//end of this file

?>[/code]

Continuously updating it, but any help would be great. The current problem is that the text file doubles in size, as so:

First run of script:
Original text.1st

Original text.second

Original text.1st

Original text.

Oh. And it doesn’t let me run the script more than 3 times. Might be a server-thing, though.

Bumpity bump and a doobity doo

Anyone?

Sponsor our Newsletter | Privacy Policy | Terms of Service