simple php, post page comments > 2 questions
Posted: Sun May 31, 2009 5:46 pm
Hi all!
I'm a linux server admin who's trying to teach myself php. I'm starting with trying to add a comment portion to pages on my site. I'm wanting to learn by hand, not grab someone else's script.
I've been working on something that seems simple, but there are two problems that I can't figure out - and I've been trying for about a week.
So, here goes:
The logic is to fill in a form with a name and a comment and hit submit. Php takes it and appends it to a flat file which is displayed with an include on the same page. I don't wish to have a "successful post" page. The idea is to be as trim and lightweight as possible. The php code is on the same html page as the form.
Problem number 1 is:
After the submit button is pressed, everything works fine - except you can hit refresh on the browser and it re-submits the same data even if nothings in the form fields. I've tried everything I can think of to clear the form, unset the variables etc... I'm not wanting to check the flat file for the same post (to keep processing low), and I don't want to use javascript.
How can I keep it from posting the same data once refresh on the browser's hit? (The direction I'd like to go is "if the field is empty, do not run" - I've tried 'if empty();' but I may not be doing it right - maybe it's not the right thing to do? Is there an html way to clear the form without javascript?
Problem number 2 is:
How can I display the last entries first with append? That may be to complex for what I'm trying to do, so I may not mess with that, but if there's a simple way - I'd like to at least explore it. I've tried 'rewind' with append to put the pointer back at the beginning of the file - but I may not be doing that right either... lol file_get_contents seems like it could take a lot of processing too - if the file gets big.
Note: I know the coding has bad form. Here's the code I have so far...
I would really appreciate any help or insight!!
Thank you!
I'm a linux server admin who's trying to teach myself php. I'm starting with trying to add a comment portion to pages on my site. I'm wanting to learn by hand, not grab someone else's script.
I've been working on something that seems simple, but there are two problems that I can't figure out - and I've been trying for about a week.
So, here goes:
The logic is to fill in a form with a name and a comment and hit submit. Php takes it and appends it to a flat file which is displayed with an include on the same page. I don't wish to have a "successful post" page. The idea is to be as trim and lightweight as possible. The php code is on the same html page as the form.
Problem number 1 is:
After the submit button is pressed, everything works fine - except you can hit refresh on the browser and it re-submits the same data even if nothings in the form fields. I've tried everything I can think of to clear the form, unset the variables etc... I'm not wanting to check the flat file for the same post (to keep processing low), and I don't want to use javascript.
How can I keep it from posting the same data once refresh on the browser's hit? (The direction I'd like to go is "if the field is empty, do not run" - I've tried 'if empty();' but I may not be doing it right - maybe it's not the right thing to do? Is there an html way to clear the form without javascript?
Problem number 2 is:
How can I display the last entries first with append? That may be to complex for what I'm trying to do, so I may not mess with that, but if there's a simple way - I'd like to at least explore it. I've tried 'rewind' with append to put the pointer back at the beginning of the file - but I may not be doing that right either... lol file_get_contents seems like it could take a lot of processing too - if the file gets big.
Note: I know the coding has bad form. Here's the code I have so far...
Code: Select all
<form method="get" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
Who:<input type="text" name="who" style="width:100px;"><br />
What?<br /><textarea name="comment" style="width:100px;"></textarea><br />
<input type="submit" value="post"></form>
<?php
$user = $_GET["who"];
$message = $_GET["comment"];
$date = date("m|d|y");
if($user != ''){
$out = fopen("data.txt", "a+");
fwrite($out, $date. " <b>$user</b> said:<br />$message<br /><br />");
fclose($out); }
?>
<?php include('data.txt'); ?>
Thank you!