PHP 5 / MySQL Programming for the Absolute Beginner
Cover
Contents
Introduction
Chapter 1: Exploring the PHP Environment
Chapter 2: Using Variables and Input
Chapter 3: Controlling Your Code with Conditions and Functions
Chapter 4: Loops and Arrays
Chapter 5: Better Arrays and String Handling
Chapter 6: Working with Files
Chapter 7: Writing Programs with Objects
Chapter 8: XML and Content Management Systems
Chapter 9: Using MySQL to Create Databases
Chapter 10: Connecting to Databases within PHP
Chapter 11: Data Normalization
Chapter 12: Building a Three-Tiered Data Application
Index
Team DDU
Nội dung
Building the HTML Page With the basic outline from Figure 2.15, it becomes clear how the Story program should be created. It should have two parts. The first is an HTML page that prompts the user for all the various words. Here’s the code for my version: <html> <head> <title>Story</title> </head> <body> <h1>Story</h1> <h3>Please fill in the blanks below, and I’ll tell you a story</h3> <form method = “post” action = “story.php”> <table border = 1> <tr> <th>Color:</th> <th> <input type = “text” name = “color” value = “”> </th> </tr> <tr> <th>Musical Instrument</th> <th> <input type = “text” name = “instrument” value = “”> </th> </tr> <tr> <th>Animal</th> <th> <input type = “text” name = “anim1” 48 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r value = “”> </th> </tr> <tr> <th>Another animal</th> <th> <input type = “text” name = “anim2” value = “”> </th> </tr> <tr> <th>Yet another animal!</th> <th> <input type = “text” name = “anim3” value = “”> </th> </tr> <tr> <th>Place</th> <th> <input type = “text” name = “place” value = “”> </th> </tr> <tr> <th>Vegetable</th> <th> <input type = “text” name = “vegetable” value = “”> </th> </tr> 49 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t 50 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r <tr> <th>A structure</th> <th> <input type = “text” name = “structure” value = “”> </th> </tr> <tr> <th>An action</th> <th> <select name = “action”> <option value = “fast asleep”>fast asleep</option> <option value = “drinking cappuccino”>drinking cappuccino</option> <option value = “wandering around aimlessly”>wandering around aimlessly</option> <option value = “doing nothing in particular”>doing nothing in particular</option> </select> </th> </tr> <tr> <td colspan = 2> <center> <input type = “submit” value = “tell me the story”> </center> </td> </tr> </table> </form> </body> </html> There’s nothing terribly exciting about the HTML. In fact, since I had the plan, I knew exactly what kinds of things I was asking for and created form elements to ask each question. I used a list box for the last question so I could put in some interest- ing suggestions. Note that I changed the order a little bit just to throw the user off. 51 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t Check a few things when you’re writing a page that connects to a script: • Make sure you’ve added an action attribute. • Ensure you’ve got the correct action attribute in the form tag. • Make sure each form element has an appropriate name attribute. • If you have radio or option objects, make sure each one has an appropriate value. • Be sure there is a submit button somewhere in your form. • Don’t forget to end your form tag. Your browser may work fine if you forget to include </form>, but you don’t know how the users’ browsers will act. Checking the Form I actually wrote two different scripts to read this form. The first one simply checks each element to make sure it received the value I expected. Here’s the first program, called storySimple.php: <html> <head> <title>Little Boy Who?</title> </head> <body> <h1>Little Boy Who?</h1> <h3>Values from the story page</h3> <table border = 1> <tr> <th>Variable</th> <th>Value</th> </tr> <tr> <th>color</th> <td><? print $color ?></td> </tr> <tr> <th>instrument</th> <td><? print $instrument ?></td> </tr> <tr> <th>anim1</th> <td><? print $anim1 ?></td> </tr> <tr> <th>anim2</th> <td><? print $anim2 ?></td> </tr> <tr> <th>anim3</th> <td><? print $anim3 ?></td> </tr> <tr> <th>place</th> <td><? print $place ?></td> </tr> <tr> <th>vegetable</th> <td><? print $vegetable ?></td> </tr> <tr> <th>structure</th> <td><? print $structure ?></td> </tr> <tr> <th>action</th> <td><? print $action ?></td> </tr> </table> <form> </html> 52 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r . the blanks below, and I’ll tell you a story</h3> <form method = “post” action = “story .php > <table border = 1> <tr> <th>Color:</th> <th> <input type. element to make sure it received the value I expected. Here’s the first program, called storySimple .php: <html> <head> <title>Little Boy Who?</title> </head> <body> <h1>Little