<?php
  session_start
();

  


  echo 
DoForm();

  
/**
   * The form control function
   * @return string the form
   */
  
function DoForm(){
    if( isset(
$_SESSION['post']) ){
      
$post unserialize($_SESSION['post']);
      
$form 'Hello, ' $post['name'] . '!';
      unset(
$_SESSION['post']);
    }else{
      
$errors ValidateForm();
      if( 
count($errors) ){
        
$form ShowForm($errors);
      }else{
        
$form ProcessForm();
      }
    }
    return 
$form;
  }

  
/**
   * Builds the markup for the form and returns it.
   * @return string form markup
   */
  
function ShowForm$errors ){
    
// First we check for errors.  If count($errors) is greater than zero, then
    // we had errors from a previous submission.
    
$error_string '';                // Define here to avoid undefined
                                       // variable errors.

    
unset($errors['empty_post']);
    if( 
count($errors) > ){
      
// Had errors, so we build an error string.  I like to use an unordered
      // list.
      
$error_string implode('</li><li>'$errors);
      
$error_string "
        <div class='form_errors'>
          The following errors were encountered while processing your request:
          <ul><li>{$error_string}</li></ul>
        </div>
      "
;
    }

    
// Now we build our defaults, we only have one field so this is easy.
    // The basic format is:
    // $field_name = isset($_POST['field_name'])
    //             ? $_POST['field_name'] : '';
    // Which means if the field_name exists in post, use it.  Otherwise use a
    // blank value.  Doing it this way will prevent PHP from reporting "index
    // undefined" errors.
    
$name = isset($_POST['name']) ? $_POST['name'] : '';

    
// Now we build our form markup.  Notice that I embed $error_string and
    // $name in the appropriate places.  Do not be alarmed if you do not fully
    // understand the form markup, that will be explained in a future article.
    
$form "
      <form method='post' action=''>
        {$error_string}
        <label>Name:</label>
        <input type='text' name='name' value='{$name}' size='16' />
        <input type='submit' name='submit' value='Say Hello!' />
      </form>
    "
;
    return 
$form;                      // And return the result...
  
}

  
/**
   * Validates the form and returns true or false.
   * @return bool true if valid, false if invalid
   */
  
function ValidateForm(){
    
$errors = array();

    
// If not yet submitted we return early, returning false.
    // Form data is available in an array named $_POST; if empty($_POST)
    // returns true then the form has not been submitted.
    
if(empty($_POST)){
      return array( 
'empty_post' => '' );        // Not submitted, return early
    
}

    
// If we have not returned, then the form was submitted and we can continue
    // with validation.

    // Get the name the user typed from the $_POST array.
    
$name $_POST['name'];

    
// User must enter a name
    
if( strlentrim$name ) ) == ){
      
// No name entered
      
$errors['name_required'] = 'Name is a required field.';
    }

    
// User must enter only alphabetic characters
    
if( !ctype_alpha$name ) ){
      
// Name is not alphabetic only
      
$errors['name_not_alpha'] = 'Name can be alphabetic characters only.';
    }

    return 
$errors;
  }

  
/**
   * Process the form.  This would be where we update the database, send
   * e-mails, or whatever else it is our form is intended to do.
   */
  
function ProcessForm(){
    
$_SESSION['post'] = serialize($_POST);
    
header('Location: ' $_SERVER['PHP_SELF']);
    exit();
  }

?>