A simple form

PHP has the ability to automatically load the values of variables from forms into variables. Learn how to display and process PHP forms.

By David Sklar and Adam Trachtenberg

One of PHP's most handy features is its ability to automatically load the values of variables from forms into variables in PHP. This makes form processing a snap.

So, if you submit a form that has an input field such as this:

<INPUT TYPE=TEXT NAME="name" VALUE="Glen Morris">

when you're processing the page with PHP, the variable $name has a value of Glen Morris.

So, you can print it out:

echo "Hi $name!";

or test its value:

if ($name == "Glen Morris") { echo "Please check your email."; }

Here's a good way to process basic forms with PHP. As an example, we'll ask people to fill out a short questionnaire with their name, email address, and a few survey questions we're interested in learning the answers to.

First we're going to divide our page into two functions. So, instead of having two separate pages (one for the form and another for the CGI script that processes it), we're going to just have one PHP page and let our logic control what to show the users.

Displaying the Form

The first function displays the form:

function display_form() {
  global $PHP_SELF;
Name:   <INPUT TYPE=TEXT NAME="name"><BR>

Favorite Cheese: <INPUT TYPE=RADIO NAME="cheese" VALUE="brie">Very soft French Brie
                 <INPUT TYPE=RADIO NAME="cheese" VALUE="cheddar">Farmhouse English Cheddar
                 <INPUT TYPE=RADIO NAME="cheese" VALUE="mozzarella">Italian Buffalo Mozzarella

Favorite Times to Eat Cheese:
                 <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="m">Morning
                 <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="n">Noon
                 <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="d">Dinner
                 <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="l">Late night

<INPUT TYPE=HIDDEN NAME="stage" VALUE="results">

Most of the text above is just the HTML necessary to create a form. However, there are also a couple of complex points, which we'll explain.

First, there's the variable $PHP_SELF. $PHP_SELF is a handy reference—its value is the URL of the current page. We set the TARGET of the form to $PHP_SELF, because we want this page to process the form. By using $PHP_SELF instead of the actual path to the page, we can rename or move our script around without a worry about explicitly setting the page location each time we reuse this piece of code.

By the way, the line

global $PHP_SELF;

means we want the global variable $PHP_SELF. Inside of a function, variables have a local scope, or have different values than variables of the same name outside of that function. If we didn't explicitly tell PHP that we wanted the global value for $PHP_SELF, we'd find that $PHP_SELF was blank.

Next, you'll notice that we slip in and out of PHP mode even inside of a function. This is completely legal. PHP is smart enough to ignore all the HTML between and just look for the next opening PHP block. This is faster than staying in PHP mode and printing the HTML using echo.

If you look at the NAMEs of the radio buttons and check boxes, you see that times[] has a pair of brackets after its name, while cheese does not. That's because radio buttons only let you choose one correct answer, so the value of cheese is just a string. Check boxes, on the other hand, let you pick any number of choices. For PHP to store all the answers, it needs to put them into an array. Adding the [] after the variable name times lets PHP know that it is an array and not a scalar.

Finally, there's a hidden variable called stage. We use $stage to tell us if we want to display the form or process the results.

Processing the Form

Next comes the form processing function, process_form().

function process_form() {
  global $name;
  global $cheese;
  global $times;

  if ($cheese == 'brie') { $cheese_message = 'I love brie.'; }
  elseif ($cheese == 'cheddar') { $cheese_message = 'Cheddar is awesome!'; }
  else { $cheese_message = 'Fresh mozzarella is divine.'; }

  $favorite_times = count($times);

  if ($favorite_times <= 1) {
    $times_message = 'You should eat cheese more often.';
  } elseif ($favorite_times > 1 && $favorite_times   $times_message = 'Those are good times to eat cheese.';
  } else {
    $times_message = 'You are eating too much cheese.';

  echo "Hello $name.";
  echo "$cheese_message $times_message";


First, as we did with $PHP_SELF, we grab the global version of our form variables. Then we check to see which cheese option was picked and create our response.

Next we use count() to find the number of choices selected as favorite times. While before, we had to check each possible value of $cheese, we're now able to compare the size of $favorite_times using less than and greater than. In case you're wondering what the && in the check "$favorite_times > 1 && $favorite_times " means, it stands for "and." So, $favorite_times must be both greater than 1 and also less than 4 for this check to be true.

At the very end, we print the person's name and our messages for him or her. You, of course, could do a variety of more interesting actions, such as storing this information in a database or querying some repository of information and spitting back the results in a pretty layout.

Putting the Form Together

Now that we've put together these two functions, we just need a bit of code to glue them together and provide the logic for our script. So, beneath display_form() and process_form() add:


if (empty($stage)) { display_form(); }
else { process_form(); }


First we check to see if $stage is empty. In PHP, a variable is considered empty if it is unset (that is, it's never been assigned a value) or if its value is nothing (that is, it has a value of "nothing" assigned to it, where nothing is either the empty string''. or zero, 0). When people first come to our page, $stage is empty. So, we want to display the form; otherwise, we process the form.

And that's all there is to using PHP to handle forms.

David Sklar is the CTO of Student.Net Publishing.

Adam Trachtenberg is the Vice President for Production of Student.Net Publishing.

Editor's Picks