PHP HTML Form Example

Use this example as a form walkthrough. We will briefly build an HTML form, and call the form data using PHP. PHP offers several methods for achieving this goal, so feel free to substitute alternative methods as you follow along. Our example will show you a method using a single .php file, combining both PHP and HTML in one simple text file, to retrieve the data and display the results. Below is a quick review of bullets, check boxes, text fields, and input fields and using them to build a form to retrieve some personal information about our user.

Building the HTML Form

Step 1 is to build the form document to retrieve user date. If you already experienced using HTML forms, this should be review, however, if not we recommend a brief visit through the Tizag HTML Forms Tutorial. The code below shows a simple html form document set up to retrieve some personal knowledge about our user.

Input Fields

Input fields are the simplest forms to grasp. As mentioned in the Forms Tutorial, just be sure to place the name attribute within the tags and specify a name for the field. Also be aware that for our form's action we have placed the $PHP_SELF super global to send our form to itself. We will be integrating more PHP code into our form as we continue on so be sure to save the file with a .php extension.

Code:

<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname">:<br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname">:<br />

Radios and Checkboxes

The catch with radio buttons lies with the value attribute. The text you place under the value attribute will be displayed by the browser when the variable is called with PHP.

Check boxes require the use of an array. PHP will automatically place the checked boxes into an array if you place [] brackets at the end of each name.

Code:

...
Gender::<br />
Male:<input type="radio" value="Male" name="gender">:<br />
Female:<input type="radio" value="Female" name="gender">:<br />
Please choose type of residence::<br />
Steak:<input type="checkbox" value="Steak" name="food[]">:<br />
Pizza:<input type="checkbox" value="Pizza" name="food[]">:<br />
Chicken:<input type="checkbox" value="Chicken" name="food[]">:<br />

Textareas

In reality, textareas are oversized input fields. Treat them the same way, just be aware of the wrap attribute and how each type of wrap will turn out. PHP relys on this attribute to display the textarea.

Code:

...
<textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea>:<br />

Drop Down Lists & Selection Lists

These two forms act very similar to the already discussed radio and checkbox selections. To name a selection form, place the name attribute within the select tags at the beginning of the form, and then place the appropriate value to fit each option.

Code:

...
Select a Level of Education:<br />
<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select>:<br />
Select your favorite time of day::<br />
<select name="TofD" size="3">
<option value="Morning">Morning</option>
<option value="Day">Day</option>
<option value="Night">Night</option></select>:<br />

Be sure to check through your code to double check for bugs or errors especially look at each name attribute to be sure your names are all correct. As far as names go, you can copy the ones shown or simply make up your own, just be sure you remember what they are. Your form should be similar to the one shown here.

Display:


First Name:
Last Name:
Gender:
Male:
Female:
Favorite Food:
Steak:
Pizza:
Chicken:

Select a Level of Education:

Select your favorite time of day: