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.2 - add_quote.php #2
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. // Lock the file:
23. flock($fp, LOCK_EX);
24.
25. fwrite($fp, "{$_POST['quote']}\n"); // Write the data. Use \r\n on Windows.
26. flock($fp, LOCK_UN); // Unlock.
27. fclose($fp); // Close the file.
28.
29. // Print a message:
30. print "<p>Your quotation has been stored.</p>";
31.
32. } else { // Could not open the file.
33. print '<p style="color: red;">Your quotation could not be stored due to a system error.</p>';
34. }
35.
36. } else { // Failed to enter a quotation.
37. print '<p style="color: red;">Please enter a quotation!</p>';
38. }
39.
40. } // End of submitted IF.
41.
42. // Leave PHP and display the form:
43. ?>
44.
45. <form action="add_quote.php" method="post">
46. <textarea name="quote" rows="5" cols="30">Enter your quotation here.</textarea>
47. <input type="submit" name="submit" value="Add This Quote!" />
48. <input type="hidden" name="submitted" value="true" />
49. </form>
50.
51. </body>
52. </html>