Now that you've got the basics of PHP variables and operators down, the second article in this series takes a look at PHP's form-processing capabilities, and introduces you to the comparison and logical operators and the "if-else" and "switch" family of conditional statements.
You'll have noticed that in all the examples we've shown you thus far, we've used two pages - a single HTML page containing the form, and a separate PHP script which processes the form input and generates appropriate output. However, PHP provides an elegant method to combine those two pages into one via the $submit variable.
You've already seen that once a form is submitted to a PHP script, all the form variables become available to PHP. Now, in addition to the user-defined variables, each time you hit the SUBMIT button on a form, PHP creates a variable named $submit. And by testing for the presence or absence of this variable, a clever PHP programmer can use a single PHP script to generate both the initial form and the output after it has been submitted.
Let's demonstrate this to you - we've rewritten the fortune cookie example above to use a single PHP file to generate both the initial drop-down list, and the subsequent fortune cookie page. We're assuming that the PHP file is named "cookie.php4"
<?
if (!$submit)
{
// if $submit doesn't exist, it implies that the form
// has not yet been submitted
// so display the first page
?>
<html>
<head>
<style type="text/css">
td {font-family: Arial;}
</style>
</head>
<body>
<font face="Arial" size="+2">
The Amazing Fortune Cookie Generator
</font>
<form method="GET" action="cookie.php4">
<table cellspacing="5" cellpadding="5" border="0">
<tr>
<td align="center">
Pick a day
</td>
<td align="right">
<select name="day">
<option value="Monday">Monday
<option value="Tuesday">Tuesday
<option value="Wednesday">Wednesday
<option value="Thursday">Thursday
<option value="Friday">Friday
<option value="Saturday">Saturday
<option value="Sunday">Sunday
</select>
</td>
</tr>
<tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Hit me!">
</td>
</tr>
</table>
</form>
</body>
</html>
<?
}
else
{
// if $submit does exist, the form has been submitted
// so process it with switch()
// the decision variable here is the day chosen by the user
switch ($day)
{
// first case
case "Monday":
$fortune = "Never make anything simple and efficient when a way can be
found to make it complex and wonderful.";
break;
// second case
case "Tuesday":
$fortune = "Life is a game of bridge -- and you've just been
finessed.";
break;
case "Wednesday":
$fortune = "What sane person could live in this world and not be
crazy?";
break;
case "Thursday":
$fortune = "Don't get mad, get interest.";
break;
case "Friday":
$fortune = "Just go with the flow control, roll with the crunches,
and, when you get a prompt, type like hell.";
break;
// if none of them match...
default:
$fortune = "Sorry, closed on the weekend";
break;
}
?>
<html>
<head>
<basefont face="Arial">
</head>
<body>
Here is your fortune for <? echo $day; ?>:
<br>
<b><? echo $fortune; ?></b>
</body>
</html>
<?
}
As you can see, the script first tests for the presence of
the $submit variable - if it doesn't find it, it assumes that the form has yet to be submitted and so displays the initial list of days.
Since the ACTION attribute of the <FORM> tag references the same PHP script, once the form has been submitted, the same script will be called to process the form input. This time, however, the $submit variable will exist, and so PHP will not display the initial page, but rather the page which contains the fortune cookie.
Note that for this to work, your
<input type="submit">
must have a NAME attribute with the value "submit", like
this: