PDA

View Full Version : (php) submitting form to text file problem



joso
August 18th, 2007, 04:48 PM
hi there,

i came across a basic php script for a guestbook and adapted it a little so it fits in with my layout, but when the form is filled out it doesn't seem write the data to the guestbook.txt file, ive checked the permissions and the .txt file currently CHMOD'd to 666

heres a link to the page:

http://www.unearth-online.net/home.php?p=readbook

this is the script in the addbook.php page :


?php
if (get_magic_quotes_gpc()) {
$name = stripslashes($name);
$email = stripslashes($email);
$rating = stripslashes($rating);
$message = stripslashes($message);
}
if ($message != '')
{
$message = str_replace ("\n","<br>",$message);
$message = strip_tags ($message, '<br>');

$newRow = '-------------------------------------------------------------------------------------------------------------------------------------------------' . ($message) . ' <br><br></td>' .
'</tr><tr><tr><td width="195" align="left" valign="top"><strong>Site Rating:</strong> ' . ($rating) . '/10</td>' .
'<td width="195" align="right" valign="top">posted on ' . date('d.m.y') . ' by <strong><a href="mailto:' . ($email) . '" class="links"> ' . ($name) . '</a></strong></td></tr></table><br>';

$oldRows = join ('', file ('guestbook.txt') );
$fileName = fopen ('guestbook.txt', 'w');
fputs ($fileName, $newRow . chr(13) . chr(10) . $oldRows);
fclose ($fileName);
}

include ("readbook.php");
?>


and i have the form action set to this :

<form action="home.php?p=addbook" method="post">


any ideas? if anyone needs some more info i can post it up.

Cheers :|

binime
August 19th, 2007, 06:31 AM
<?php
if (get_magic_quotes_gpc()) {
$name = stripslashes($name);
$email = stripslashes($email);
$rating = stripslashes($rating);
$message = stripslashes($message);
}
if ($message != '')
{
$message = str_replace ("\n","<br>",$message);
$message = strip_tags ($message, '<br>');

$newRow = '-------------------------------------------------------------------------------------------------------------------------------------------------' . ($message) . ' <br><br></td>' .
'</tr><tr><tr><td width="195" align="left" valign="top"><strong>Site Rating:</strong> ' . ($rating) . '/10</td>' .
'<td width="195" align="right" valign="top">posted on ' . date('d.m.y') . ' by <strong><a href="mailto:' . ($email) . '" class="links"> ' . ($name) . '</a></strong></td></tr></table><br>';

// $oldRows = join ('', file ('guestbook.txt') );
if ( is_writable( "guestbook.txt" ) )
{
$fileName = fopen ('guestbook.txt', 'r+');
$towrite = $newRow . chr(13) . chr(10);//$oldRows;
fwrite($fileName, $towrite, strlen( $towrite ));
fclose ($fileName);
}
else
{
die ( "Guestbook.txt is not writable!" );
}
}

include ("readbook.php");
?>
changed the open mode and added in a error testing for is_writable on guestbook.txt. try it, let me know!

joso
August 19th, 2007, 09:48 PM
nah man, i pasted that code in place of mine and still no joy. If its any help i've zipped all the files and attached them to this post, might be easier for anyone to have a look at it.

also put it up online without all the scripts on the actual site as i thought that might be causing something but still no joy:

http://www.unearth-online.net/book/readbook.php

:crying:

binime
August 20th, 2007, 01:52 AM
Ok well i think i got it to work.

http://www.tloz.co.uk/kirupa/200807/guestbook/pic1.png

http://www.tloz.co.uk/kirupa/200807/guestbook/pic2.png


http://www.tloz.co.uk/kirupa/200807/guestbook/addbook.zip

Hope it works for you, as you can see i got it working for me!

You definately need to put some error checking on the form though!

simplistik
August 20th, 2007, 07:51 AM
bleh, that's one piece of ugly script though :lol:, to many tables for no reason.

joso
August 20th, 2007, 09:55 AM
binime, i downloaded your zip file and it seems to be empty? looks like you got it running though

simplistik
August 20th, 2007, 10:07 AM
binime, i downloaded your zip file and it seems to be empty? looks like you got it running thoughNah, it works I looked at it.