...

View Full Version : New to php - need some help



stophon4
04-11-2004, 06:23 PM
Hi, I just started learning php and I need some help, I am trying to make a guestbook, I thought this code would work but it doesn't:


<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor=gold>
<font size=7><center><b><u>Guest Book</u></b></font><br><br>
<?php
include("guestbook.php");

if($submitted);
{
if((Name=="")&&(comments=="")){
echo ("You forgot to sign your name and comments!");
}
$file = fopen("guestbook.php", "a");
fputs($file, '<hr>');
fputs($file, $Name);
fputs($file, '<HR>');
fputs($file, '<center>');
fputs($file, $comments);
fputs($file, '<hr><hr>');
fclose($file);

}
echo ('<form action="veiw_bg.php" method="post">Sign The Guestbook<Br><hr>Your Name: <input type=text MAXLENGTH=10 Name="Name"><br>Comments: <textarea rows=10 cols=100 Name="comments"></textarea><br><input name="submitted" type="hidden" value="true"><input type="submit" Value="Leave your comments"></form>')
?>
</body>
</html>

Len Whistler
04-11-2004, 06:33 PM
I think your syntax for the include statement is wrong try


include "guestbook.php";

Also this project isn't a good one to learn php on you should try simple php projects. I also noticed at least one odd spelling mistake "veiw_bg.php" shouldn't that be view?


Leonard Whistler
www.stubby.ca

Nightfire
04-11-2004, 06:49 PM
Include is right ;)

Ok, here's what it should be


<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor=gold>
<font size=7><center><b><u>Guest Book</u></b></font><br><br>
<?php
include("guestbook.php");

if(isset($_POST['submitted']));
{
if(($_POST['Name']=="")&&($_POST['comments']=="")){
echo ("You forgot to sign your name and comments!");
}
$file = fopen("guestbook.php", "a");
fputs($file, '<hr>');
fputs($file, $_POST['Name']);
fputs($file, '<HR>');
fputs($file, '<center>');
fputs($file, $_POST['comments']);
fputs($file, '<hr><hr>');
fclose($file);

}else{
echo ('<form action="veiw_bg.php" method="post">Sign The Guestbook<Br><hr>Your Name: <input type=text MAXLENGTH=10 Name="Name"><br>Comments: <textarea rows=10 cols=100 Name="comments"></textarea><br><input type="submit" Value="Leave your comments" name="submitted"></form>')
}
?>
</body>
</html>

gsnedders
04-11-2004, 07:07 PM
You can use
include ('guestbook.php');, everthough Nightfire has already answered your question.

stophon4
04-11-2004, 09:12 PM
Yeah, actually about 20 minutes after I made that post I relised that all I had to do is put a else in after the if comments and name are there box, This wasn't a hard project to start on. I aready know JavaScript and HTML so this wasn't very hard. But php is really cool. It is a ton better than JavaScript. I am tryin to learn this. I might have some more questions about php. But I really like it. Man I could create a really good forum with this...

stophon4
04-11-2004, 09:48 PM
ARGH!! more errors:


<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor=gold>
<font size=7><center><b><u>Guest Book</u></b></font><br><br>
<?php
include("guestbook.php");

if($submitted);
{
if(($_POST['Name']=="")&&($_POST['comments']=="")){
echo ("You forgot to sign your name and comments!");
}
else
{
$file = fopen("guestbook.php", "a");
fputs($file, '<table bgcolor="grey"><tr><td>');
fputs($file, '<hr>');
fputs($file, 'Name: &nbsp;&nbsp;');
fputs($file, $_POST['Name']);
fputs($file, '<HR>');
fputs($file, '<center>');
fputs($file, 'Comments:<br>');
fputs($file, $_POST['comments']);
fputs($file, '<hr><hr>');
fputs($file, '</table>');
fclose($file);
}
}
echo ('<form action="veiw_bg.php" method="post">Sign The Guestbook<Br><hr>Your Name: <input type=text MAXLENGTH=10 Name="Name"><br>Comments: <textarea rows=10 cols=100 Name="comments"></textarea><br><input name="submitted" type="hidden" value="true"><input type="submit" Value="Leave your comments"></form>')
?>
</body>
</html>

it works fine, but you have to hit refresh to see what happened and that causes it to send again and....

dniwebdesign
04-11-2004, 10:28 PM
Include a "no-cache" meta tag, as well you could put the "Sign Guestbook" part on a different page and after it is posted just redirect back to your guestbook.php. (Not that hard)

Nightfire
04-11-2004, 10:44 PM
Here you go. Copy all of this


<?php
if(isset($_POST['submitted']));
{
if(($_POST['Name']=="")&&($_POST['comments']=="")){
echo ("You forgot to sign your name and comments!");
}else{
$file = fopen("guestbook.php", "a");
fputs($file, '<hr>');
fputs($file, $_POST['Name']);
fputs($file, '<HR>');
fputs($file, '<center>');
fputs($file, $_POST['comments']);
fputs($file, '<hr><hr>');
fclose($file);
header("Location:veiw_bg.php");
}
}
?>
<html>
<head>
<title>Guestbook</title>
</head>
<body bgcolor=gold>
<font size=7><center><b><u>Guest Book</u></b></font><br><br>
<?php
include("guestbook.php");

echo ('<form action="veiw_bg.php" method="post">Sign The Guestbook<Br><hr>Your Name: <input type=text MAXLENGTH=10 Name="Name"><br>Comments: <textarea rows=10 cols=100 Name="comments"></textarea><br><input type="submit" Value="Leave your comments" name="submitted"></form>')

?>
</body>
</html>

stophon4
04-11-2004, 11:00 PM
Warning: Cannot modify header information - headers already sent by (output started at /home/www/stophon4/guestbook/veiw_bg.php:3) in /home/www/stophon4/guestbook/veiw_bg.php on line 19

I fot that error...

EDIT: it works fine, your code worked, I just modified it slightly. Except the only problem is when you load the page there is nothin in the two of them. So it brings up the error.

EDIT2: That's fixed :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum