PDA

View Full Version : PHP - Comment Code


Sikawtic
07-14-2006, 12:46 AM
I would like a simple comment code that has a box for your name and email (I have this right now: <form method = "post" action = "comments.php">
<table>
<tr><td>Name:</td><td><input type="text" name="sn" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td colspan="2">Comment:</td></tr>
<tr><td colspan="2"><input type="hidden" name="comments">
<textarea id="textarea" name="comments" /></td></tr>
</table>
<input type="submit" value="Post" />
</form>

Then I want it to check if the name isn't blank, and if it isn't... post it on the page below the content box (same page).

So it would end up like:

Name:
Email:
Type your comment here.


COMMENT #1 posted by NAME (EMAIL) on DATE.

COMMENT #2 posted by NAME (EMAIL) on DATE.

Etc... you all know what I'm talking about already, probably :P

Black.Ice
07-14-2006, 1:45 AM
Do you actually want the code for this, or do you want help making it?

The reason I ask is because if someone is learning to code, I usually help them by providing tips rather than doing a simple project like this for them -- it helps them learn.

Markpyro
07-14-2006, 12:05 PM
First of all, your form structure needs a bit of tuning up:

<form method = "post" action = "comments.php">
<table>
<tr><td>Name:</td><td><input type="text" name="sn" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td colspan="2">Comment:</td></tr>
<tr><td colspan="2"><input type="hidden" name="comments">
<textarea id="textarea" name="comments" /></textarea></td></tr>
</table>
<input type="submit" value="Post" />
</form>





I also need help writing code that I dont feel like learning, if that is your case. For this particular problem, if you want all the comments to appear, you either need to store the data to a database, or a text file. To make this easy, I'm writing this to modify a text file.


<?php
error_reporting(E_ALL);
// This code should be put on a page SEPERATE
// from the page you want to display comments on,
// so something like insert.php. Change the form as needed

// the function below, htmlspecialchars
// sanatizes the input so nobody
// can insert malicious code
// To a point, though it also means
// That you cannot use HTML within the comment



if ($_POST['comments'] == "" || $_POST['sn'] == "")
{

echo "You have either not included your name or a comment. Please go back and try again";

}
else
{
$content['comment'] = htmlspecialchars($_POST['comments']);
$content['name'] = htmlspecialchars($_POST['sn']);
$content['email'] = htmlspecialchars($_POST['email']);
$full = $content['comment'] . "<br> by: <b>" . $content['name'] . "</b><br> Email: <b>" . $content['email'] . "</b><br><br>";
$filename = "comments.txt";

$o=fopen($filename,"a");
fwrite($o,$full);
fclose($o);
echo "Your Comment has been Posted<br> <a href='view.php'>Continue</a><br>";

}
?>

And the following should be on the page that you want the comments to appear on:

<?php
// The following shows the comments. Just nest the code wherever you want
// the comments to appear. If you want the comments to be
// within table tags or whatnot, just ask. The code is easily
// modifiable

$filename = "comments.txt";


$f=fopen($filename,"r");
$data=fread($f,filesize($filename));
fclose($f);
echo "<p>" . nl2br($data) . "</p>";
?>



And that is the simplest I can make it. I didnt include things like limits on comment size or anything like that, but I can add it if you want. So, basically, you have three pages.

xxxx.html has the form
comments.php has the code that inserts the comment into the text file
view.php is the code that displays the comments

I assume you're proficient enough to change the code to cater it to your needs as far as filenames, but if not just post what you need and I'll fix it.



The reason I ask is because if someone is learning to code, I usually help them by providing tips rather than doing a simple project like this for them -- it helps them learn

I learned ^_^. It was fun :P


If you want to test it-
http://pyrom.net/test/blah.php

Seal
07-14-2006, 12:29 PM
i have a somewhat clean article(or blog)+comment system. uses a database though. want it for study? i don't really want to continue developing it since im starting to prefer MVC-style frameworks like Django for active sites.

Sikawtic
07-14-2006, 2:05 PM
Thanks all, but I just found a tut and "learned' (copied mostly) the shoutbox they have and am going to tweak it to fit my likings.

MP: I have the / char at the end... I don't need a seperate /textarea ... see:
<textarea id="textarea" name="comments" />

Seal: I could take that I spose, more knowledge the better, right?

Markpyro
07-14-2006, 2:08 PM
Yeah, but it doesnt work. I put it into the browser and the rest of the code was inside the textbox.

Sikawtic
07-14-2006, 2:13 PM
Hmmm, trixie it is.

Markpyro
07-14-2006, 2:17 PM
Observe. (http://pyrom.net/test/crap.php)

MP: I have the / char at the end... I don't need a seperate /textarea ... see:
Not all tags work like that. Some need a definate ending, just like you couldnt have a paragraph tag like this "<p />" with no ending. It needs to be defined.

Seal
07-14-2006, 4:19 PM
i recall my offer, i can't efficiently package the stuff without rebuilding most of it.

Sikawtic
07-18-2006, 1:07 PM
:(hmmm