Forum Moderators: coopster

Message Too Old, No Replies

Combining form handler script with the main script

         

Sam_C

4:30 pm on Sep 30, 2006 (gmt 0)

10+ Year Member



Ok, bear with me, I'm only on the second chapter of my beginner's PHP book and I'm well and truly stuck. ;) Here's what I have so far...

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. :)

jatar_k

5:12 pm on Sep 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



all you need to do in the second script is

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.

Sam_C

7:13 pm on Sep 30, 2006 (gmt 0)

10+ Year Member



Ah, thanks. So, would I still have both scripts/pages, but with a textarea on the first page and a textarea and the submitted text on the second..?

Don't be afraid to give me the "Dummies Guide" explanation, that's my level. :)

jatar_k

3:46 pm on Oct 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you planning to allow them to resubmit from that second page when you show it in a textarea again?

if not then yes, it would be exactly as you said

Sam_C

5:22 pm on Oct 1, 2006 (gmt 0)

10+ Year Member



Well, the original aim was to have just a single page but the sort of setup seen on this forum would be ok I suppose - when you click Reply you see a page with a textarea (empty in this case) and when you click on Preview it shows a page with a textarea containing the text and the text that was entered on the previous page just above it.

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]