PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


PHP Forms

PHP With HTML Forms

12/01/2000

In the last exciting episode of the PHP Phanatics, we explored the inner workings of PHP's unique $GLOBALS array. Several sharp-eyed readers pointed out a bug in the script dumping the contents of the $GLOBALS array. If an array element was itself an array, the display listed that fact rather than the contents of the component array. The displayed line looked like:

$GLOBALS[HTTP_GET_VARS]=Array

In my defense I plead confusion. There were elements on both sides of the array. In addition, the vertical alignment was less than perfect. It was very difficult determining which array component a pointer was addressing.

A generalized array-dumping function is discussed at the end of this article. The developed function displays any one- or two-dimensional array.

In this episode we'll continue our exploration of PHP forms, becoming more aggressive and bolder as we go.

Expanding our bag of form tricks

Last time our introductory form contained two text inputs and a multiple-choice drop menu. The first form had only rudimentary error checking, displaying only a message indicating that a required field was not entered. In the form about to be developed, we'll add a few radio buttons, a check box, a text area, and a drop menu limited to allowable states.

A few additional goodies will include a fairly robust error-handling function that displays specific errors. The best parts of the new form are functions to create radio button and text box groupings containing any number of buttons or boxes. Both functions also will display the current selected status of a radio button or text box grouping if available.

Drop menu builder

Script form-two.php contains a function named MakeStateDropMenu(). As the name suggests, the function builds a state drop menu. For good measure, possessions with postal service abbreviations and APO/FPO options are included. The function has one required and one optional parameter. The first parameter is the form name associated with the drop menu, such as "CustomerState." The optional parameter contains the state abbreviation to be initially selected. If the second parameter is not specified, no state is selected.

Menu building starts with:

$DropMenu=<<<dropmenu
<select name="$Name" size="1">
  <option value="AL">Alabama</option>
 ...
  <option value="AP">AP-APO/FPO</option>
</select>
DropMenu;

It terminates with:

if ($Selected) {
  $DropMenu = preg_replace("|\"$Selected\">*|",
     "\"$Selected\" SELECTED>",$DropMenu);
} # if ($Selected)
return $DropMenu;

The function preg_replace() performs a Perl-type regular expression replacement. Let's assume "NJ" was passed as the second parameter when calling the function. The entire menu string is searched for "NJ"> and upon finding:

<option value="NJ">New Jersey</option>

"NJ"> is replaced with "NJ" SELECTED>. Cool, no?

You can prime the pump with something like:

$Selected = ($State) ? $State : "NJ";

If $State, the drop menu NAME field, has a value, use it. If not, use NJ. If no state is to be pre-selected, call the function with something like:

$StateMenu = MakeStateDropMenu("State",$Selected);

Drop menus, when the choices are pre-determined and fairly limited, insure that any user selection is valid.

A variation on the drop menu theme could be employed to populate the SELECT options from a database.

Radio buttons

The purpose of a radio button grouping is to select one item from a mutually exclusive group. Although the buttons can be initialized with no selection, a maximum of one item is selected. If one button is selected and another button is checked, the selection on the first button will disappear. The NAME parameters for a radio button grouping are all identical, and the content of the variable is determined by the VALUE clause of the selected button.

A generalized radio button builder would be nice, so let's build one. Our first set of radio buttons on the new form requests the submitter's gender. The function SetRadioButtons() has four parameters; the first two are required and the second two are optional. The first positional parameter contain the value used for the NAME parameter. All radio buttons in a grouping share a common NAME clause. The content of the VALUE clause associated with the user's selection is transmitted to the script.

The second positional parameter is the prompt value to be initially checked. The second parameter should contain a null field ("") if there is to be no initial selection. The third parameter (optional) is an array of possible values. The default is (1,0). The last parameter (optional) is an array of prompts. The default is ("Yes","No"). The relative position in the prompt array should correspond with the values array. There can be any number of buttons in the radio button grouping. Calling the function with:

SetRadioButtons("Gender","Male",array("M","F"),array("Male:","Female:"));

produces the following HTML code:

Male: <input type="RADIO" name="Gender" value="M" checked>
Female: <input type="RADIO" name="Gender" value="F">

If the form is to be redisplayed with the submitted value displayed, call the function with:

SetRadioButtons("Gender",$Gender,array("M","F"),array("Male:","Female:"));

If the function is called using the shortest parameters list:

SetRadioButtons("Married","1") the returned string would be:

Yes: <input type="RADIO" name="Married" value="1" checked>
No: <input type="RADIO" name="Married" value="0">

Check boxes

The purpose of a check box grouping is allowing the selection of zero or more items from a list whose items are not mutually exclusive. "Check all that apply" comes to mind as a good heading. As with radio buttons, check boxes can be initially selected.

The function MakeCheckBoxes() uses several tricks to manage the group. Although the NAME parameters in a group of check boxes can all be different, that would be very difficult to generalize. In the MakeCheckBoxes() function, the NAME clause of each contains an array element. The function has three parameters; the first two are required and the last is optional. The first parameter holds the value used for the NAME cause. The second parameter is an array of values used for the prompt and VALUE clauses. The last, optional parameter is the array boxes initially selected. The form in form-two.php asks, "What Operating Systems have you used? Check all that apply." The function is called with:

$OperatingSystemBoxes = MakeCheckBoxes(
  "OperatingSystem",array("Unix","Linux","Windows 2000","Windows 9x","Other"),
   $OperatingSystem);

Note that the last parameter is an array. Since the result of this check box group upon submitting the form is an array, the array variable can be used for the last parameter. If some items are to be initially selected, the last parameter might be:

array("Value1","Value2") ;

If only one box is to be generated, the call might look like:

$OneBox = MakeCheckBoxes("OneBox",array("Unix"),array("Unix"));

Pages: 1, 2

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: