1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4. <head>
5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6. <title>Add A Quotation</title>
7. </head>
8. <body>
9. <?php // Script 11.1 - add_quote.php
10. /* This script displays and handles an HTML form. This script takes text input and stores it in a text file. */
11.
12. // Check for a form submission:
13. if (isset($_POST['submitted'])) { // Handle form.
14.
15. if ( !empty($_POST['quote']) && ($_POST['quote'] != 'Enter your quotation here.') ) { // Need some thing to write.
16.
17. if ($fp = fopen ('../quotes.txt', 'ab')) { // Try to open the file.
18.
19. // Set the encoding:
20. stream_encoding($fp, 'utf-8');
21.
22. fwrite($fp, "{$_POST['quote']}\n"); // Write the data. Use \r\n on Windows.
23. fclose($fp); // Close the file.
24.
25. // Print a message:
26. print "<p>Your quotation has been stored.</p>";
27.
28. } else { // Could not open the file.
29. print '<p style="color: red;">Your quotation could not be stored due to a system error.</p>';
30. }
31.
32. } else { // Failed to enter a quotation.
33. print '<p style="color: red;">Please enter a quotation!</p>';
34. }
35.
36. } // End of submitted IF.
37.
38. // Leave PHP and display the form:
39. ?>
40.
41. <form action="add_quote.php" method="post">
42. <textarea name="quote" rows="5" cols="30">Enter your quotation here.</textarea>
43. <input type="submit" name="submit" value="Add This Quote!" />
44. <input type="hidden" name="submitted" value="true" />
45. </form>
46.
47. </body>
48. </html>