PHP DevCenter
oreilly.comSafari Books Online.Conferences.


PHP Forms

Phorm Manager


Previously in PHP Phanatics:

The Universal Web Form Processor

PHP With HTML Forms

Using PHP to Interpret Forms

In the last article, the Phanatic was leading an expedition into a largely uncharted area. The conceptual project was a generalized form handler. The task's starting point was Matt Wright's famous "formmail" and included adding many more bells and whistles. While putting together the last tome, time and space were closing in on us, and in an effort not to strain the Phanatic's rapidly aging brain, we pulled into shore for a rest. Let's review the project's design goals before setting out to sea again.

Design goals

The desirable conceptual characteristics of the form handler and the developed script should, based upon the form designer's choice:

  • Display a neatly formatted output of the form's variables
  • E-mail a neatly formatted output of the form's variables to a designated recipient
  • Optionally send a neatly formatted receipt to the form's submitter
  • At the conclusion of the process, redirect the user's browser to a designated URL, or display a "thank you" message
  • Validate designated fields for non-null, e-mail address, ZIP code, and numeric
  • Allow user selection of cosmetic properties
  • Allow users maximum latitude in the selection of form names
  • Permit section headers
  • Include selected environment variables in the output display

Some of the task's more challenging programming considerations are:

  • Detect if the form was submitted using the GET or POST method
  • Delineate between HIDDEN control variables and unknown user variables
  • For security reasons, only allow e-mail requests from designated domains
  • Deal with form elements having multiple values
  • Validate selected fields such as e-mail addresses, ZIP codes, and numeric data.

Some of these issues were stowed away in the last article, so you might want to review that tome before continuing.

The Phanatic is a little late with this column because, as usual, things take longer than estimated, especially with a Herculean task like this one. This is a major script and as such deserves a special name. After burning some midnight oil and injecting enough sugar to kill a small colony of rodents, we have it: "The Phorm Manager," possibly a little grandiose, but what's the point of being a Phanatic if one is not somewhat pompous.

The Swiss Army chainsaw

The Phanatic has such low self-esteem that he wants to be all things to all people. As a consequence, this project is about to emerge (degenerate?) into a Swiss Army chainsaw. You may not want all of the Phorm Manager's features in your application, so feel free to take out what you think is the Phanatic's application candy.

The Phorm Manager, being somewhat schizophrenic, can cope with different missions. There are three task handlers, namely:

  • Test
  • Mail
  • Mail and Acknowledge

Forms, especially those of the complicated persuasion, require a good bit of development work. It is frequently useful to test the form before placing it in production. A little cosmetic tweaking is somewhat like chicken soup when you're sick: It may not help but it can't hurt.

System demonstration

Before getting too involved in the inner workings of the Phorm Manager, it might help your understanding if you play with the demo for a bit. The demo form and processing script are designed to show off some of the Manager's features. If you enter your own e-mail address, you will get an e-mail copy of the finished product. There are a pair of radio buttons allowing switching modes between Test and Mail. Test mode displays the input data as well as the data help in the Form, System, and Environment arrays.

In addition, the first and last names will not allow null input. The e-mail address and ZIP code will be validated for correct format. The test numeric will pass only numeric data. More on these further on in this column; suffice to say, the form designer can determine which, if any, fields are to be non-null, e-mail addresses, ZIP codes, or numeric.

One feature used in the demo form requires some additional discussion so you can use the concept in your future form use. A PHP array can be populated with an explicit subscript such as:

$A[] = "abc";
$A[] = "xyz";

and so on. If a form variable can contains multiple vales and will subsequently be processed by a PHP script, the following code may be used:

Unix <INPUT TYPE="CHECKBOX" NAME="OperatingSystem[]" VALUE="Unix">
Linux <INPUT TYPE="CHECKBOX" NAME="OperatingSystem[]" VALUE="Linux">
Windows <INPUT TYPE="CHECKBOX" NAME="OperatingSystem[]" VALUE="Windows">
Other <INPUT TYPE="CHECKBOX" NAME="OperatingSystem[]" VALUE="Other">

The GetFormData converts the array to a comma-separated list with the following:

foreach ($FormVariables as $Name=>$Value) {
if (is_array($Value)) $FormVariables[$Name] = join(",",$Value);

One note of caution: If all items are checked, $OperatingSystem[0] contains "Unix" and $OperatingSystem[3] contains "Other." However, if only "Windows" is checked, it will be $OperatingSystem[0], and $OperatingSystem[1..3] are undefined.

So, take a break and go to our demo here.(Editor note: For anti-spam reasons, we can not allow the e-mail to actually get sent, so you will see an error message. )

PHP mail

Let's ease into this session with a look at PHP's mail() function. Firstly, mail() is configured to use sendmail on a Unix box, and on other systems it will look for a local or remote mail server. If you have problems with mail, start by checking the SMTP directive in the php.ini file.

The basic syntax of the mail function is:

mail(string to, string subject, string message[, string additional headers]);

A simple mail() call, with apologies to Elizabeth Barrett Browning, might be:

mail("","The Subject is Love PHP",
"My message is: How much do I love PHP? Let me count the ways");

Note, a From: address is not required. The optional fourth parameter is a list of headers, each of which must be terminated with the CRLF (Carriage Return Line Feed) characters \r\n. Here is an example using both From: and Reply-to: headers.

    "From: $FromAddress\r\nReply-to:$ReplyAddress");

We'll return to mail shortly.

Internal data management

The Phorm Manager has two major conceptual arrays, $FormVariables and $SystemVariables. These arrays comprise the brain and nervous system of the Phorm Manager. Care must be exercised when designing your form. The difference between a system variable, a user form variable, or an environment variable can be as simple as one mistyped character. The test suite displays the names and contents of the form variables, the system (hidden field) variables, and environment variables without sending mail.

The heartbeat of this application is a series of hidden form fields. The hidden fields supply control and cosmetic information, as opposed to user-supplied data, for the Phorm Manager. There is only one required hidden field, the e-mail address to receive the e-mailed data. A typical form entry might be:


Let's take a look at how FormVariables and SystemVariables come into being and how they interrelate.

Pages: 1, 2, 3

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: