<?php
//======================================================================================
// This script takes form7.html page, validates the fields, repopulate the fields,
// and diplays '*' and error messages where appropriate.
//
// The idea is to separate the php code completely from the html code
//
// The html code will need to use:
//     %SELF%                           to substitute the php script name
//     %fieldname%                      to substitute the value of the field
//     %fieldname.error%                to flag the field if in error
//     %fieldname.fieldvalue.checked%   to flag radio or checkbox to be checked
//     %fieldname.fieldvalue.selected%  to flag select list to be selected
//     %MESSAGE%                        to substitute where the error message go
//======================================================================================
error_reporting(0);

//  $url = 'form7.html';
    $url = '/home/sultans/web/php/demo/4http/form7.template';       # to make it work in PHP*Tester

    if ($_POST)                                                     # if data was collected in _POST array 
        validate();     
        
    display();

//=============================================================================
function validate()
{
    global $error, $msg;

//      foreach($_REQUEST as $fieldName => $fieldValue)                 //for generic processing if desired
//      {
//          if ((is_string($fieldValue) && $fieldValue == '') ||
//              ( is_array($fieldValue) && count($fieldValue) == 0))                
//          {
//              $error['fieldName'] = '*';
//          }     
//      }

        if (preg_match('/^\s*$/', $_REQUEST['firstname']))	//if nothing but spaces or nulls          
               $error['firstname'] = '*';

        if (preg_match('/^\s*$/', $_REQUEST['lastname']))          
               $error['lastname']  = '*';

        if (preg_match('/^\s*$/', $_REQUEST['address']))          
               $error['address']   = '*';

        if (count($_REQUEST['flavor'])  == 0)           //an array 
               $error['flavor']    = '*';

        if (count($_REQUEST['topping']) == 0)           //an array
               $error['topping']    = '*';
 
        if ($_REQUEST['creditCard'] == '')
               $error['creditCard'] = '*';

        if (count($error) > 0)
            $msg  = 'Please enter required field(s) above!';     
        else
            $msg  = 'Thank you for your order!';      
}

//=============================================================================
function display()
{
    global $url, $error, $msg;

    $html = fopen($url, 'r');

    if (! $html) die("Cannot retrieve $url");

    while($line = fgets($html))                         //read 1 line at a time
    {                   
        if (preg_match('/%(.+)%/',$line,$match))
        { 
            $nodes = explode('.',$match[1]);
            $field_name   = $nodes[0];
            $field_value  = $nodes[1];
            $field_option = $nodes[2];

            if ($field_name && !$field_value && !$field_option)                 
            {
                if (strtoupper($field_name) == 'SELF')
//                  $var = 'form7.php';                                         
                    $var = $_SERVER['PHP_SELF'];                                //to make it work in PHP*Tester

                elseif (strtoupper($field_name) == 'MESSAGE')
                    $var = $msg;

                else
                    $var = $_REQUEST[$field_name];                              //input type text, textarea, etc.
            } 

            if ($field_name && $field_value && $field_option)                   
            {                
                if (is_string($_REQUEST[$field_name]))                          //radio button
                    $var = $_REQUEST[$field_name]==$field_value ? $field_option : ""; 

                if (is_array($_REQUEST[$field_name]))                           //checkboxes or select list
                    $var = in_array($field_value,$_REQUEST[$field_name]) ? $field_option : ""; 
            } 

            if ($field_name && strtolower($field_value) == 'error')            //substituting for error flags                  
            {
                $var = $error[$field_name];
            } 

            $line = preg_replace('/%.+%/', $var, $line);        //substitute
        }
        print $line;
    }
    fclose($html);
}            
?>
<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->