• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Why wont this PHP script work?

Akalon

NLC
NLC
I just can't get this guestbook script to work, i've tried everything.... here it is:


post.php
PHP:
<html>
<head>
<title>Post A Message</title>
<body bgcolor="#0099CC">
<font face="Times New Roman" size="7"><center>My Guestbook</center></font><br><br>

<? $footer = "<br>\n<br>\n<br>\n<center>Copyright 2001, Brendan Cilia.  v.01</center>" ?>

<? If(!$submit){ ?>
<font face="Arial" size="4"><center>Post A Message</center></font><br><br>
<form name="guestbook" method="post" action="<? echo $PHP_SELF;?>">
<font face="Tahoma, Arial" size="2">Nickname: <input type="text" name="nickname" size="25"><br>
Email Address: <input type="text" name="email" size="25"><br>
URL (Optional): <input type="text" name="url" size="25"><br>
Comments: <textarea name="comments" cols="45" rows="4"></textarea><br><br></font>
<input type="submit" name="submit" value="Post...">
</form>
<? echo "<font face=\"Tahoma, Arial\" size=\"2\">$footer</font>";?>

<? } elseif(($nickname == "") OR ($email == "") OR ($comments == "")){ ?>
<font face="Arial" size="2" color="red"><strong>Error! It appears you have left one or all of the required fields blank!</strong></font><br><br>
<font face="Arial" size="4"><center>Post A Message</center></font><br><br>
<form name="guestbook" method="post" action="<? echo $PHP_SELF;?>">
<font face="Tahoma, Arial" size="2">Nickname: <input type="text" name="nickname" size="25"><br>
Email Address: <input type="text" name="email" size="25"><br>
URL (Optional): <input type="text" name="url" size="25"><br>
Comments: <textarea name="comments" cols="45" rows="4"></textarea><br><br></font>
<input type="submit" name="submit" value="Post...">
</form>
<? echo "<font face=\"Tahoma, Arial\" size=\"2\">$footer</font>";?>


<? } else { ?>
<font face="Arial" size="5"><center>Confirm Message:</center></font><br><br>
<form name="guestbook method="post" action="guestbook.php">
<font size="2" face="Tahoma, Arial"><b>Nickname:</b> <? echo "$nickname";?><br>
<b>Email Address:</b> <? echo "$email";?><br>
<b>URL:</b> <? echo "$url";?><br>
<b>Comments:</b> <? echo "$comments";?></font><br><br>
<input type="submit" name="confirm" value="Confirm">
<? echo "<font face=\"Tahoma, Arial\" size=\"2\">$footer</font>";?>
<? } ?>


</body>
</html>


guestbook.php
PHP:
<html>
<head>
<title>Post A Message</title>
<body bgcolor="#0099CC">
<font face="Times New Roman" size="7"><center>My Guestbook</center></font><br><br>

<?
$fp=fopen ("messages.txt","w+");
fwrite($fp,$nickname,$email,$url,$comments);
fclose($fp);
?>


</body>
</html>


Can someone please tell me why it isn't putting the information on messages.txt?
 
I'm not a PHP expert but I think your problem is in guestbook.php, try to change it to:
PHP:
<html>
<head>
<title>Post A Message</title>
<body bgcolor="#0099CC">
<font face="Times New Roman" size="7"><center>My Guestbook</center></font><br><br>

<?
// The new guestbook entry output, you can modify it if you want
$message = "<font size=\"2\" face=\"Tahoma, Arial\"><b>Nickname:</b> $nickname<br>
<b>Email Address:</b> $email<br>
<b>URL:</b> $url<br>
<b>Comments:</b> $comments</font><br><br>\n";

$fp=fopen ("messages.txt","a");
fwrite($fp,$message);
fclose($fp);
?>


</body>
</html>

That is going to write the new entry at the end of the file, if you want to write it at the beginning, before all the other entries, try this:
PHP:
<html>
<head>
<title>Post A Message</title>
<body bgcolor="#0099CC">
<font face="Times New Roman" size="7"><center>My Guestbook</center></font><br><br>

<?
// The new guestbook entry output, you can modify it if you want
$message = "<font size=\"2\" face=\"Tahoma, Arial\"><b>Nickname:</b> $nickname<br>
<b>Email Address:</b> $email<br>
<b>URL:</b> $url<br>
<b>Comments:</b> $comments</font><br><br>\n";

$fcontents = join ( '', file ( "messages.txt")); 
$fp = fopen ("messages.txt", "w");
fwrite ($fp, "$message\n");
fwrite ($fp, $fcontents);
fclose ($fp);
if ($entries != 0 && $entries > 0){
$data = file ("messages.txt"); 
$fpp = fopen ("messages.txt", "w");
for ($i=0; $i < $count; $i++){
fwrite ($fpp, $data[$i]);
}
}
?> //Edited, forgot to close it, thanks neural!

</body>
</html>

Hope this helps.
 
Last edited:
The first script you gave me worked but all it printed in messages.txt was:

<font size="2" face="Tahoma, Arial"><b>Nickname:</b> <br>
<b>Email Address:</b> <br>
<b>URL:</b> <br>
<b>Comments:</b> </font><br><br>

not what the actual users typed in...

the second script gave me this:
Parse error: parse error in /home/extremebb/www/cgi-bin/guestbook.php on line 28

Any Ideas? Thanks Satelk
 
Parse error: parse error in /home/extremebb/www/cgi-bin/guestbook.php on line 28

satelk just fogot ?> to close php tag before

</body>
</html>

in your messages.txt script writes everything but email, naickname etc?
 
Ok, try to change the last part of post.php to:
PHP:
<? } else { ?>
<font face="Arial" size="5"><center>Confirm Message:</center></font><br><br>
<form name="guestbook method="post" action="guestbook.php">
<input type=hidden name=nickname value"<? echo $nickname;?>">
<input type=hidden name=email value"<? echo $email;?>">
<input type=hidden name=url value"<? echo $url;?>">
<input type=hidden name=comments value"<? echo $comments;?>">
<font size="2" face="Tahoma, Arial"><b>Nickname:</b> <? echo "$nickname";?><br>
<b>Email Address:</b> <? echo "$email";?><br>
<b>URL:</b> <? echo "$url";?><br>
<b>Comments:</b> <? echo "$comments";?></font><br><br>
<input type="submit" name="confirm" value="Confirm">
<? echo "<font face=\"Tahoma, Arial\" size=\"2\">$footer</font>";?>
<? } ?>

It didn't write the variables because they didn't exist anymore, now they will be transfered with the form because of the hidden inputs.
 
Last edited:
Ok, its working now except it still doesn't print the variables, instead it prints twice:

<font size="2" face="Tahoma, Arial"><b>Nickname:</b> <br>
<b>Email Address:</b> <br>
<b>URL:</b> <br>
<b>Comments:</b> </font><br><br>

<font size="2" face="Tahoma, Arial"><b>Nickname:</b> <br>
<b>Email Address:</b> <br>
<b>URL:</b> <br>
<b>Comments:</b> </font><br><br>


Thanks for helping guys :)
 
I am not sure if this will help but ...

In the file post.php, the first 'if' statement is
PHP:
<? If($submit) { ?>

Mabye it should be
PHP:
<? if($submit) { ?>

I am not sure if it is case sensitive
 
Nah, don't think that's the problem cos that part of the script works... thanks anyways :classic2:
 
Back
Top