I set out to write this by hand to learn and not just upload someone's pre-packaged code. However, because I'm new to developing with php I'm finding myself googling descriptions of what I'd like the code to do - and it's not working very well...
I've been at it for about 2 weeks now. A few days ago, I admitted I need help (hehe) and created an account here and posted. So far you all have been very helpful, and not critical. Thank you! With that, I'll jump into it.
-- Here's my goal: --
A small and simple comment section on pages of my site. A user should be able to add the comment via a form, and the comment should display on the same page.
The page only uses xhtml and php. The database is a flat file (the reasons I'm not using mysql are here).
The code for the form, processing and display should all be in the same file.
Because I'm a minimalist, I don't want an admin page, a bad word filter, smileys etc... I'm also trying to make it as few lines as possible.
I would like to have protection against xss and code injection with spam protection too (simple captcha?). I haven't gotten any of that figured out yet.
To stay organized, my naming convention is index.php indcom.txt gallery.php galcom.txt etc...
-- Here are some of the problems I've ran into: --
Without a thank you page, a refresh re-submits the same data even with nothing in the form.
*I've tried methods (meta and php) to clear the form, clear the cache and set variables to be empty / null with no luck. I'm probably doing it wrong. This is how I would like to do it though.
With a thank you page, but the php code on the form page - I can't figure out how to tell php to grab from the form, if the form's action is the thank you page, but I don't have the refresh re-submit problem.
With a thank you page with the php code on the thank you page, I have no code problems, but now have 3 files (form, processing and flat file).
I can't get captcha to work (I've installed gd w/ libs).
If I leave error reporting to all and on, I get undefined index notices.
Those are just some of the issues I've ran into.
-- Extra stuff: --
I know the code below doesn't have the right tabs, and please keep in mind I'm still learning.
Lastly, if you guys set out to do the same thing, with a clean slate, the same goals and constraints as above (few lines, same page...), how would you all go about it? Could you give examples of how you would do it?
(Also, thanks in advance for any help or insight!!)
Here's what I have so far, and it only 1/2 works - it can all go in the can of course.
Code: Select all
<?php include('indcom.txt'); ?>Code: Select all
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
Who:<br /><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>Code: Select all
<?php
$user = $_POST["who"]; //Gets the user from the from and assigns it to $user.
$message = $_POST["comment"]; //Gets the comment from the form and assigns it to $message.
$date = date("[ Md 'y ]"); //Assigns the date at that time to $date.
if($user != ''){ //Says if nothing in field, write nothing instead of empty breaks.
$out = fopen("indcom.txt", "a+"); //Opens file to append (add) to.
rewind($out); //Should put cursor at top to write for last comment first - doesn't work.
fwrite($out, $date. " <b>$user</b> said:<br /><i>$message</i><br /><br />");
fclose($out); }
?>