Here's an example of forms handling, combining much of what we've seen previously, and adding in a couple new items. The form below has no PHP code: that's in the program that processes the form. I called the form "sem.html":
<HTML>
<HEAD>
<TITLE>Test Form</TITLE>
</HEAD>
<H1>Test Form</H1>
<FORM METHOD="POST" ACTION="sem.php3">
<INPUT TYPE=HIDDEN NAME="Request" VALUE="Book">
<P>You are:
<blockquote>
<INPUT TYPE="RADIO" NAME="Status" VALUE="Blue"> Blue
<BR><INPUT TYPE="RADIO" NAME="Status" VALUE="Yellow"> Yellow
<BR><INPUT TYPE="RADIO" NAME="Status" VALUE="Green"> Green
</blockquote>
<PRE>
Your Name: <INPUT NAME="Name" SIZE="30">
Email: <INPUT NAME="Email" Size="30">
Pick a date: <SELECT NAME="Month">
<option>January
<option>February
<option>March
<option>April
<option>May
<option>June
<option>July
<option>August
<option>September
<option>October
<option>November
<option>December
</SELECT> Day: <input name="Day" size="2" maxlength="2">
</pre>
<HR ALIGN=CENTER WIDTH =60%>
<DD>Any comments:
<CENTER><TEXTAREA NAME="Comments" ROWS=5 COLS=50 WRAP=PHYSICAL>
</TEXTAREA></CENTER>
<BR>
<CENTER><INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Send Request">
<INPUT TYPE="RESET" NAME="RESET" VALUE="Clear this form"></CENTER>
</FORM>
<HR>
</BODY>
</HTML>
|
This is a standard form with several different elements, the only unusual thing about it is the choice of form handler: "sem.php3". CGI is usually used to handle forms, and it's usually written in Perl. Here is the "sem.php3" code:
<HTML>
<HEAD>
<TITLE>Form Output</TITLE>
</HEAD>
<BODY>
<P>Hi <?php echo $Name?>.
<?php
/* The form returns the following information:
Request (embedded)
Status (required)
Name
Email
Month
Day (required)
Comments
*/
$GoodToGo=TRUE;
$ErrorMessage="";
if (ereg("^[[:blank:]]*$", "$Status")) {
$GoodToGo=FALSE;
$ErrorMessage = $ErrorMessage . "<LI>You need to give us something to work with here - what colour are you?";
}
if (($Day <= 0) || ($Day >= 32)) {
$GoodToGo=FALSE;
$ErrorMessage = $ErrorMessage . "<LI>What kind of day is that? Try something between 1 and 31.";
}
if ($GoodToGo) {
mail ("you@mail.university.edu",
"$Request Request",
"
$Request Request
----------------------------------------------
Name: $Name (E-mail: $Email)
With Colour: $Status
----------------------------------------------
Chose:
$Month $Day
Comments:
$Comments
");
echo "<P>Thanks. Your Request has been submitted.";
echo "<P>If you have further requests to make, you can hit the Back ";
echo "button in your browser to be returned to the form. You can change ";
echo "the data and resubmit. ";
echo "<HR>";
} else {
echo "<P>Your request cannot be processed for the following reasons:";
echo "<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=5>";
echo "<TR><TD BGCOLOR=\"#FFFACD\"><UL>";
echo "<P>$ErrorMessage <BR>";
echo "</UL></TD></TR></TABLE>";
echo "<P><B>Please hit the BACK button in your browser to fill out the
rest of the details in your form. Thanks.</B><BR>";
echo "<HR>";
}
?>
</BODY>
</HTML>
|
Note that all the variables from the form are available to this new page, and the output is a combination of HTML and PHP. "If" uses a regular expression to check if $Status is blank:
ereg("^[[:blank:]]*$", "$Status") |
"ereg" returns a true or false, based on whether it finds the first string it is supplied with in the second string given to it. This is a extended regular expression meaning "if $Status consists of nothing at all, or only tabs and spaces, return true." If it returns true, we add a note about it to the $ErrorMessage variable. This is followed by a check to make sure that we have a reasonable date. The check tests if the date is less than 1 or (using "||" which is logical OR) greater than 31. If we have an unreasonable date, we add a comment to the error message. In both cases, if there is a problem, $GoodToGo is set to FALSE.
The program now checks if $GoodToGo is TRUE - if it is, the program sends mail using the mail function:
mail (string to, string subject, string message) |
and a message of acknowledgement is displayed in the client's browser. Note that the "mail" command is carried over several lines, ending only when it's closed with "); .
If $GoodToGo is FALSE, no mail is sent, and the $ErrorMessage that was built up is displayed.