Forum Moderators: coopster

Message Too Old, No Replies

simple guestbook with no mysql

can't seem to figure it out

         

fuzzbuzz

11:05 am on Jan 7, 2007 (gmt 0)

10+ Year Member



I'm kinda a n00bie in php still, and can't figure out howto get the form fields to be sent to guestbook.txt .. i found some scripts on the net, but they doesn't have the thing or look i need.. I need a simple form with "name" "email" and the message. and that the outcome looks a little like this:

Name(with mailto link) - date
----------------------------------------------------------------------
Message: (and then the message)

New message

.. can anybody help me out?

fuzzbuzz

11:44 am on Jan 7, 2007 (gmt 0)

10+ Year Member



i found a script now that works, but I got some few troubles with it.. all the new messages comes at the bottom of the .txt file.. and they should be at the top.. and also it was without date.. how can I intergrate that?
here's what the code looks like:

<title></title>
<?
$filename = 'install.php';

if (file_exists($filename)) {
echo("<META HTTP-EQUIV=Refresh CONTENT='1; URL=install.php'>");
}
if(isset($_POST['Submit']))
{
?><table width="100%" border="0" cellspacing="1" bordercolor="#000000" bgcolor="#333333">
<tr>
<td width="432"><?

$dataf = "posts.txt";
$name = $_POST['name'];
$message = $_POST['message'];
$mail = $_POST['mail'];

if (!$name)
{ $name = "Anonymous"; }
else $name .= " Skrev:";

//word censors
$message = str_replace("#*$!","****",$message);
$message = str_replace("bitch","****",$message);
$message = str_replace("#*$!","****",$message);
$message = str_replace("#*$!","****",$message);
$message = str_replace("bull#*$!","****",$message);
$message = str_replace("#*$!","****",$message);
$message = stripslashes($message);
$comfile = file($dataf);

if ($message!= "") {$file = fopen("$dataf","a");
$write = fwrite($file,"<table width='90%' border='0' align='center' cellspacing='1' bgcolor='#333333'>
<tr>
<td bgcolor='#000000'><a href=\"mailto:$mail\">$name</a></td>
</tr>
<tr>
<td bgcolor='#000000'>$message</td>
</tr>
</table><br><br>");
fclose($file);

}
else
{
echo"Du må fylle ut meldingsfeltet! ";
}

if($write == TRUE)
{
echo "Meldingen har blitt lag til!";
}
else
{
echo"Feil i meldingen";
}
?></td>
</tr>
</table><? }?>
<br>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bordercolor="#000000" bgcolor="#333333">
<tr>
<td width="432"><form name="guestbook" action="" method="post">
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Navn</td>
<td><input name="name" type="text" id="name" /></td>
</tr>
<tr>
<td>Mail</td>
<td><label>
<input name="mail" type="text" id="mail" />
</label></td>
</tr>
<tr>
<td>Melding</td>
<td><textarea name="message" id="message"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Legg til" />
<input type="reset" name="Reset" value="Fjern" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form></td>
</tr>
</table>
<br>
<br>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
<tr>
<td class="style3"><em><strong>Meldinger</strong></em>::</td>
</tr>
<tr>
<td><? include("posts.txt");?></td>
</tr>
</table>

can anybody help out? :)

jatar_k

12:38 pm on Jan 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld fuzzbuzz,

this is part that needs to be changed

if ($message!= "") {$file = fopen("$dataf","a");
$write = fwrite($file,"<table width='90%' border='0' align='center' cellspacing='1' bgcolor='#333333'>
<tr>
<td bgcolor='#000000'><a href=\"mailto:$mail\">$name</a></td>
</tr>
<tr>
<td bgcolor='#000000'>$message</td>
</tr>
</table><br><br>");
fclose($file);

you need to read the data from the file into a variable
I would then close the file, then reopen with a 'w' flag
then write your new comment to the file
then write the old data back to the file
then close the file again

that will invert the comments

fuzzbuzz

1:07 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



okey :)

thx :) but like I said I'm a n00b :P so I have no idea how to do that :) and where to edit ... :// if you could give me a starter i would appreciate that =)

jatar_k

1:17 pm on Jan 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, something like this might work, though I have not tested it at all

if ($message!= "") {
$myolddata = file_get_contents [php.net]($dataf);
$file = fopen("$dataf","w");
fwrite($file,"<table width='90%' border='0' align='center' cellspacing='1' bgcolor='#333333'>
<tr>
<td bgcolor='#000000'><a href=\"mailto:$mail\">$name</a></td>
</tr>
<tr>
<td bgcolor='#000000'>$message</td>
</tr>
</table><br><br>");
fwrite($file,$myolddata);
fclose($file);

- I grabbed the content with file_get_contents
- then opened the file with 'w' instead of 'a', this opens the file for writing only, truncates the file to zero length and puts the pointer at the beginning of the file
- I then left the original write
- then added a second write to put the old info back in the file

fuzzbuzz

10:44 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



thx alot :D

it worked fine :)) only one bug with it.. when you now post a coment, the "error" comes up that it's an error in the message, but the message is still posted and works, so i just changed the error message to that the message was added.. :)
so thanx alot for helping me and for explaining what you did so I could learn from it :D

scriptmasterdel

11:55 pm on Jan 7, 2007 (gmt 0)

10+ Year Member



If i could leave on suggestion to expand this thread a little, my personal opinion of this is if you are going to be writing to a text file then i would be better for a maintainance point of view if only the relative information was stored in the text file and not any html.

Otherwise, nice going guys and thanks for listening!

Del

jatar_k

12:12 am on Jan 8, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah, the error, see I don't read norwegian (my wild guess at the language, apologies if I'm wrong) so parts of the script kind of blurred for me

to make that message work just change this line

fwrite($file,"<table width='90%' border='0' align='center' cellspacing='1' bgcolor='#333333'>

back to

$write = fwrite($file,"<table width='90%' border='0' align='center' cellspacing='1' bgcolor='#333333'>

my fault

I also agree with Del but I didn't want to get too far into that at the moment. Once that text file gets ridiculously huge you will want to find a way to reduce it. Then we can go into comma/tab/char delimited files or flat file databases. ;)

fuzzbuzz

11:46 am on Jan 8, 2007 (gmt 0)

10+ Year Member



okey, thx alot again :))

and yes, it's Norwegian ;)

atm it's not a problem with a huge .txt file, I think it would be best if like the comments were 10 per page or something.. But if it gets to big now, I'll just save it into another text file and delete the entries in posts.txt.. :)