<html>
<head>
<title>Order Ice Cream</title>
</head>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>
<h2>Process using same page. Does not maintain data entry fields</h2>
<h3>Output uses echo whenever needed in the HTML form<br>
    Validation checks for spaces as well</h3>

<?php
    error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);      //all but warnings & notices

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

//=============================================================================
    function validate()
    {
        global $firstname, $lastname, $address, $flavor, $topping, $creditCard;   
        global $name_error,$addr_error,$flav_error,$top_error,$cc_error,$msg;   

        $firstname  = $_POST['firstname'];              #get HTML form entry fields 
        $lastname   = $_POST['lastname']; 
        $address    = $_POST['address'];
        $flavor     = $_POST['flavor'];                 #select list --> array
        $topping    = $_POST['topping'];                #checkboxes --> array
        $creditCard = $_POST['creditCard'];

        $msg;                                           #create an empty msg variable

        if (is_array($flavor))                          
            $flavor  = implode(',' , $flavor);          #flatten out the array          
        if (is_array($topping))                         
            $topping = implode(',' , $topping);         #flatten out the array          

        if (preg_match('/^\s*$/',$firstname) || preg_match('/^\s*$/',$lastname)) {
            $msg        = 'error';     
            $name_error = '*';
        }
        if (preg_match('/^\s*$/',$address)) {           #if nothing but spaces or nulls
            $msg        = 'error';     
            $addr_error = '*';
        }
        if ($flavor == '') {
            $msg        = 'error';     
            $flav_error = '*';
        }
        if ($topping == '') {
            $msg       = 'error';     
            $top_error = '*';
        }
        if ($creditCard[0] == '') {
            $msg      = 'error';     
            $cc_error = '*';
        }
        if ($msg == 'error')
            $msg  = 'Please enter required field(s) above!';     
        else
        {
            $msg  = "$firstname $lastname at $address <br/>";
            $msg .= "ordered $flavor with $topping <br/>";
            $msg .= "using credit card $creditCard <br/>";
        }     
    }

//=============================================================================
// Printing the form uses simple HTML output rather than PHP print output
//=============================================================================
    function display()
    {
        global $name_error,$addr_error,$flav_error,$top_error,$cc_error,$msg;   
?>      

        <form method=POST action=<?php echo $_SERVER['PHP_SELF'] ?> >      <!-- use $_SERVER[PHP_SELF] to make it work in PHP*Tester -->                              
        <fieldset style='width:570px; height:310px; border-color:gold'>
        <legend>Enter Fields Below</legend>

        <table bgcolor=eeeeee>
        <colgroup  span=1 width=155></colgroup>
        <tr>
        <td><b>First Name 
        <td><input type=text name=firstname>
        <tr>
        <td><b>Last Name <font color=red><?php echo $name_error ?></font>
        <td><input type=text name=lastname>    
        <tr>
        <td><b>Enter Address <font color=red><?php echo $addr_error ?></font>
        <td><textarea name=address rows=4 cols=47></textarea>
        <tr>
        <td><b>Ice Cream Flavor <font color=red><?php echo $flav_error ?></font>
        <td><select name="flavor[]" SIZE="4" multiple="multiple">
                   <option value="vanilla"       > Vanilla</option>
                   <option value="chocolate"     > Chocolate</option>
                   <option value="strawberry"    > Strawberry</option>
                   <option value="butter-pecan"  > Butter Pecan</option>
                   <option value="rocky-road"    > Rocky Road</option>
                   <option value="french-vanilla"> French Vanilla</option>
                   <option value="pistachio"     > Pistachio</option>
            </select>
        <tr>
        <td><b>Select Topping <font color=red><?php echo $top_error ?></font>
        <td>
        <input type="checkbox" name="topping[]" value="hotFudge"    />Hot Fudge 
        <input type="checkbox" name="topping[]" value="sprinkles"   />Sprinkles 
        <input type="checkbox" name="topping[]" value="nuts"        />Nuts 
        <input type="checkbox" name="topping[]" value="whippedCream"/>Whipped Cream 
        <tr>
        <td><b>Choose Credit Card <font color=red><?php echo $cc_error ?></font>
        <td>
        <input type="radio" name="creditCard" value="visa"       />Visa 
        <input type="radio" name="creditCard" value="master-card"/>Master Card 
        <input type="radio" name="creditCard" value="amex"       />Amex 
        <tr>
        <td>
        <input type="submit" value="Place Order">
        <td>
        <input type="reset" value="Cancel">
        </table>
        </fieldset>
        <br><font color="red"><?php echo $msg ?></font>
        <br/>
        </form>
<?php
    }
?>
<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->
</body>
</html>