Forms Processing in PHP 1
When just starting with PHP, it may be a bit daunting to create a form and do something with the data that's entered. Even looking at sample code, the process looks "backwards."

Let's begin with the basic steps for the process:

    initialize
    process form - user has clicked submit
    display form - not submitted yet

You will note that the same script that shows the form, also processes it. It does not have to be this way, but for our example we will do it this way.

In the initialization part of the script you would do your session management, get some variables from a database, include function files, or other housekeeping tasks. For very simple scripts, there may be nothing to do.

In the processing part of the script you evaluate the data you got from the form, massage it if needed, validate it and then do something with it. Two common tasks are to record the data in a database, or send an email. When finished, you may exit the script, redirect to a new page, or display the form again for more input.

In the display part of the script you show the html form. Use the method="POST" without the action clause. That will cause the script to execute again when submit is clicked. You may display default values by creating them in the initialization part of the script.

Let's look at some pseudo code to further illustrate the process:

<?php
// initialize
if isset(submit_button)
{
    validate variables
    take some action
    exit
}
?>
display the form

How about an example?

Source code for the example.