Link to home
Start Free TrialLog in
Avatar of assaultkitty
assaultkitty

asked on

PHP Guest book

I would like to work with the this program.  I need to make changes to make it work. 1. I am not getting the Thank you for signing the guest book message when I sign the guest book.  2.  I cannot get the Show Guest book to show the guest book names.  Can you help me?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Show Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$Book=fopen("guestbook.txt", "ab");
if (is_readable($Book))
{
    $GuestBook=readfile("guestbook.txt");
    echo "<pre>$GuestBook</pre>";
}
else
    echo "<p>The $Book file cannot be read.</p>";

fclose($Book);
?>
<hr /><p>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Sign Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_POST['first_name']) || empty($_POST
['last_name']))
      echo "<p>You must enter your first and last
      name.  Click your browser's Back button to
      return to the Guest Book.</p>\n";
else
{
      $FirstName = addslashes($_POST['first_name']);
      $LastName = addslashes($_POST['last_name']);
      $GuestName = fopen("guestbook.txt", "ab");
      if (is_writeable("guestbook.txt"))
      {
            if (fwrite($GuestBook, $LastName . ", " .
                   $FirstName . "\n"))
            echo "<p>Thank you for signing our
                  guest book!</p>\n";
            else
                  echo "<p>Cannot add your name to the
                        guest book.</p>\n";
      }
      else
            echo "<p>Cannot write to the file.</p>\n";
      fclose($GuestBook);
}
?>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<h2>Enter your name to sign our guest book</h2>
<form method= "POST action="SignGuestBook.php">
<p>First Name<input type="text" name="first_name"
/></p>
<p>Last Name<input type="text" name="last_name"
/></p>
<p><input type="submit" value="Submit" /></p>
</form>
<p><a href ="ShowGuestBook.php">Show Guest Book
</a></p>
</body>
</html>
SOLUTION
Avatar of effx
effx
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>  I am not getting the Thank you for signing the guest book message when I sign the guest book.

Is the script saying "Cannot write to the file." or "Cannot add your name to the guest book." then?

>>  I cannot get the Show Guest book to show the guest book names.

If it's not showing the names then it's either not reading the file or it's not adding the names to the file in the first place.  Tell me what happens when you submit the following form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Guest Book</title>
<meta http-equiv="content-type"
      content="text/html; charset=iso-8859-1" />
</head>
<body>
<h2>Enter your name to sign our guest book</h2>
<form method= "POST action="SignGuestBook.php">
<p>First Name<input type="text" name="first_name"
/></p>
<p>Last Name<input type="text" name="last_name"
/></p>
<p><input type="submit" value="Submit" /></p>
</form>
<p><a href ="ShowGuestBook.php">Show Guest Book
</a></p>
</body>
</html> 

Open in new window

Avatar of assaultkitty
assaultkitty

ASKER

Thank you.  I am not at my computer.  I am school.  I will check about.  Sorry about the delay.  
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This part needed some changes.  I cannot test it but I believe this is all you need.  Maybe add <pre> tags in the HTML

It really will help you a lot if you get into the habit of looking up the PHP functions so you know what the take as input, and what they do, and what they produce as a return value.  Example here:
http://php.net/manual/en/function.readfile.php
<!DOCTYPE html>
<head>
<title>Show Guest Book</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php readfile("guestbook.txt"); ?>
<hr />
</body>
</html>

Open in new window

I think this may be about right for the third script.  Here is some annotation.

1. The elaborate DOCTYPE declaration is archaic.  Eliminate it and use the HTML5 doctype.
2. You do not need to use addslashes() on these strings.  Even if they were destined for a data base, addslashes() is not the right function.  You want to use the context-aware escape function like mysql_real_escape_string().
3. It's wise to use the curly braces for your control structures.  It's also wise to line them up so the code is easy to read.
4. When writing to the file system, use the predefined constant for the end of line, PHP_EOL.  It will make your code more portable and if you ever move from Windows to Linux to Mac, you will be glad you did it the right way.
5. You do not need fclose() - the script termination algorithm will close the file for you.
6. You almost certainly would benefit from using error_reporting(E_ALL) in your PHP scripts.
7. Note that I changed the error messages here, so you can see which error condition might prevent you from writing into the guest book.
8. Note the fopen() mode parameter.  The POST data is character data, so you may not need the "b".  See http://php.net/manual/en/function.fopen.php
9. You do not need to copy the data strings from $_POST.  You get no extra credit for proliferating variables.  
10. You might consider adding some sanity checks to the incoming data.  Consider using http://php.net/manual/en/function.filter-var.php
11. You might consider putting the name of the file into a PHP comment line so you can keep track of which script you are working on.

See also the discussion and recommendation of the SitePoint book here:
https://www.experts-exchange.com/questions/27477387/GuestBook.html

Best of luck with your project, ~Ray
<!DOCTYPE html>
<head>
<title>Sign Guest Book</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
if (empty($_POST['first_name']) || empty($_POST['last_name']))
{
    echo "<p>You must enter your first and last
    name.  Click your browser's Back button to
    return to the Guest Book.</p>\n";
}
else
{
    $fp = fopen("guestbook.txt", "a");
    if ($fp)
    {
        if (fwrite($fp, "{$_POST["LastName"]}, {$_POST["FirstName"]}" . PHP_EOL))
        {
            echo "<p>Thank you for signing our guest book!</p>\n";
        }
        else
        {
            echo "<p>Cannot use fwrite() to add your name to the guest book.</p>\n";
        }
    }
    else
    {
        echo "<p>Cannot use fopen() to open the guest book.</p>\n";
    }
}
?>
</body>
</html>

Open in new window

Wizard,  I do not get a thank you message.  I just stay on the guest book html page.
Guru, I change the text to a and I do not get a change.
Savant, I tried your suggestions and I do not have any change in my program.
I tried your suggestions and I do not have any change in my program.  I will install these scripts that I just posted and test them, then post any changes back here.  In the mean time, please post your scripts the way you have them now, after making the changes I suggested.  Thanks, ~Ray
Array index keys are case-sensitive.  Last_Name is not the same as last_name.  I was able to discover the issue by adding error_reporting(E_ALL) to the script that processed the form.  See note #6 of my earlier post.

Here is GuestBook.php
<!DOCTYPE html>
<head>
<title>Guest Book</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h2>Enter your name to sign our guest book</h2>
<form method="post" action="SignGuestBook.php">
<p>First Name<input type="text" name="first_name" /></p>
<p>Last Name<input type="text" name="last_name" /></p>
<p><input type="submit" value="Submit" /></p>
</form>
<p><a href="ShowGuestBook.php">Show Guest Book</a></p>
</body>
</html>

Open in new window


Here is ShowGuestBook.php
<!DOCTYPE html>
<head>
<title>Show Guest Book</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php readfile("guestbook.txt"); ?>
<hr />
</body>
</html>

Open in new window


Here is SignGuestBook.php  
<!DOCTYPE html>
<head>
<title>Sign Guest Book</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
error_reporting(E_ALL); // ************************************** ALWAYS USE THIS
if (empty($_POST['first_name']) || empty($_POST['last_name']))
{
    echo "<p>You must enter your first and last
    name.  Click your browser's Back button to
    return to the Guest Book.</p>\n";
}
else
{
    $fp = fopen("guestbook.txt", "a");
    if ($fp)
    {
        if (fwrite($fp, "{$_POST["last_name"]}, {$_POST["first_name"]}" . PHP_EOL))
        {
            echo "<p>Thank you for signing our guest book!</p>\n";
        }
        else
        {
            echo "<p>Cannot use fwrite() to add your name to the guest book.</p>\n";
        }
    }
    else
    {
        echo "<p>Cannot use fopen() to open the guest book.</p>\n";
    }
}
?>
</body>
</html>

Open in new window


All done!
Guys, I found the error.  It was the GuestName.  It is suppose to be GuestBook.  Thanks for your help.
My program works!!!