Forum Moderators: coopster
The contents of usertext.txt is read and it appears in a form textarea:
<html><head></head><body>
<?php
$filename = "usertext.txt";
$file = fopen( $filename, "r" );
$filesize = filesize( $filename );
$text = fread( $file, $filesize );
fclose( $file );
$text = stripslashes($text);
echo( "<form action=\"append.php\" method=\"post\">" );
echo( "<textarea cols=\"60\" rows=\"10\" name=\"string\">$text</textarea>");
echo( "<br /><input type=\"submit\" value=\"Submit\"></form>");
?>
</body></html>
When the form is submitted, the second page/script takes the contents of the textarea (which may have been changed by the user) and writes it onto the page:
append.php:
<?php
$filename = "usertext.txt";
$file = fopen( $filename, "w" );
$string = $_POST['string'];
fwrite( $file, $string);
fclose( $file );
$FileContents = file_get_contents($filename);
$FileContents = nl2br($FileContents);
$FileContents = stripslashes($FileContents);
echo ( $FileContents );
?>
What I'd like to do is have everything on the one page so that when the form is submitted, the updated text appears below the textarea and in the textarea. I've experimented with $_SERVER['PHP_SELF'] but I'm getting nowhere fast. Time to ask an expert. :)
open the file for reading
pull out the data to a var
close the file
reopen the file for writing
write data
you can then use the posted data and the read data to display the 2 sets of data
the problem, among many, with submitting to itself is you have reposting problems, on refresh it executes again.
I'm just getting a bit lost in the code at the moment... it serves me right for jumping in at the deep end. ;)
[edited by: Sam_C at 5:25 pm (utc) on Oct. 1, 2006]