Question Description
Need help with my Computer Science question – Im studying for my class.
Exercise 5-2 In this project, you will create a Web page that allows visitors to your site to sign a guest book that is saved to a text ?le. Ensure that the Projects directory has read and write permissions for everyone.
1. Create a new document in your text editor and type the declaration, element, document head, and element. Use the strict DTD and Guest Book as the content of the element.
2. Add the following text and elements to the document body: Enter your name to sign our guest book First Name Last Name
Show Guest Book 3. Save the document as GuestBook.html in the Projects direc- tory for Chapter 5. 4. Create a new document in your text editor and type the declaration, element, document head, and element. Use the strict DTD and Sign Guest Book as the content of the element. 5. Add the following script section to the document body: 6. Add the following if statement to the script section to check whether the user ?lled in the ?rst name and last name ?elds: if (empty($_POST['?rst_name']) || empty($_ POST['last_name'])) echo "You must enter your ?rst and last name. Click your browser's Back button to return to the Guest Book.n"; 7. Add the following else clause to the end of the script section. The statements in the else clause use the fwrite() function to add visitor names to a text ?le named guestbook.txt. else { $FirstName = addslashes($_POST['?rst_name']); $LastName = addslashes($_POST['last_name']); $GuestBook = fopen("guestbook.txt", "ab"); if (is_writeable("guestbook.txt")) { if (fwrite($GuestBook, $LastName . ", " . $FirstName . "n")) echo "Thank you for signing our guest book!n"; else echo "Cannot add your name to the guest book.n"; }
else echo "Cannot write to the ?le.n"; fclose($GuestBook); } 8. Save the document as SignGuestBook.php in the Projects directory for Chapter 5. 291 9. Create a document named ShowGuestBook.php that dis- plays the names of visitors who have signed the guest book. Use the read?le() function to display the contents of the guestbook.txt ?le. Note that you will need to use the element for Web browsers to recognize the line breaks. 10. Open GuestBook.html in your Web browser by enter- ing the following URL: http:///PHP_Projects/ Chapter.05/Projects/GuestBook.html. Test the form to see if you can write data to and read data from the guestbook.txt ?le. 11. Close your Web browser window.