- Predefined Variables
- A Script to Acquire User Input
- Accessing Form Input with User Defined Arrays
- Accessing Form Input with Built-In Arrays
- Distinguishing Between GET and POST Transactions
- Combining HTML and PHP Code on a Single Page
- Using Hidden Fields to Save State
- Redirecting the User
- File Upload Forms and Scripts
- Summary
- Q&A
- Workshop
Combining HTML and PHP Code on a Single Page
In some circumstances, you may want to include form-parsing code on the same page as a hard-coded HTML form. Such a combination can be useful if you need to present the same form to the user more than once. You would have more flexibility if you were to write the entire page dynamically, of course, but you would miss out on one of the great strengths of PHP. The more standard HTML you can leave in your pages, the easier they will be for designers and page builders to amend without reference to you. You should avoid scattering substantial chunks of PHP code throughout your documents, however. This will make them hard to read and maintain. Where possible you should create functions that can be called from within your HTML code, and can be reused in other projects.
For the following examples, imagine that we are creating a site that teaches basic math to preschool children and have been asked to create a script that takes a number from form input and tells the user whether it is larger or smaller than a predefined integer.
Listing 9.8 creates the HTML. For this example, we need only a single text field, but even so, we'll include a little PHP.
Listing 9.8 An HTML Form that Calls Itself
1: <html> 2: <head> 3: <title>Listing 9.8 An HTML form that calls itself</title> 4: </head> 5: <body> 6: <form method="POST"> 7: Type your guess here: <input type="text" name="guess"> 8: </form> 9: </body> 10: </html>
Whatever we name the page that contains this form, the fact that we have left out the action attribute of the form element will mean that the form will be submitted back to its own url.
NOTE
Almost all browsers will submit a form to its current page if the form element's action attribute is omitted. You can, however, explicitly tell the browser to submit a form back to its own document by using the predefined $PHP_SELF variable.
<form action="<?php print $PHP_SELF?>">
The script in Listing 9.8 will not produce any output. In Listing 9.9, we begin to build up the PHP element of the page. First, we need to define the number that the user will guess. In a fully working version, we would probably randomly generate this, but for now we will keep it simple. We assign '42' to the $num_to_guess variable on line 2. Next, we need to decide whether the form has been submitted; otherwise, we will attempt to assess variables that have not yet been made available. We can test for submission by testing for the existence of the variable $guess. $guess will have been made available as a global variable if your script has been sent a "guess" parameter. If this isn't present, we can safely assume that the user has arrived at the page without submitting a form. If the value is present, we can go ahead and test the value it contains. The test for the presence of the $guess variable takes place on line 4.
Listing 9.9 A PHP Number Guessing Script
1: <?php 2: $num_to_guess = 42; 3: $message = ""; 4: if ( ! isset( $guess ) ) 5: $message = "Welcome to the guessing machine!"; 6: elseif ( $guess > $num_to_guess ) 7: $message = "$guess is too big! Try a smaller number"; 8: elseif ( $guess < $num_to_guess ) 9: $message = "$guess is too small! Try a larger number"; 10: else // must be equivalent 11: $message = "Well done!"; 12: 13: ?> 14: <html> 15: <head> 16: <title>Listing 9.9 A PHP number guessing script</title> 17: </head> 18: <body> 19: <h1> 20: <?php print $message ?> 21: </h1> 22: <form method="POST"> 23: Type your guess here: <input type="text" name="guess"> 24: </form> 25: </body> 26: </html>
The bulk of this script consists of an if statement that determines which string to assign to the variable $message. If the $guess variable has not been set, we assume that the user has arrived for the first time and assign a welcome string to the $message variable on line 5.
Otherwise, we test the $guess variable against the number we have stored in $num_to_ guess, and assign advice to $message accordingly. We test whether $guess is larger than $num_to_guess on line 6, and whether it is smaller than $num_to_guess on line 8. If $guess is neither larger nor smaller than $num_to_guess, we can assume that it is equivalent and assign a congratulations message to the variable (line 11). Now all we need to do is print the $message variable within the body of the HTML.
There are a few more additions yet, but you can probably see how easy it would be to hand this page over to a designer. He can make it beautiful without having to disturb the programming in any way.