<html>
<head>
<title>Order Ice Cream</title>
</head>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>
<h2>Process using same page. Maintains all data entry fields</h2>

<?php

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

    $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'];

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

    display();

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

        if (is_array($flavor))                          
            $flavor2  = implode(',' , $flavor);         #flatten out the array          
        if (is_array($topping))                         
            $topping2 = 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 ($flavor2 == '') {
            $msg        = 'error';     
            $flav_error = '*';
        }
        if ($topping2 == '') {
            $msg       = 'error';     
            $top_error = '*';
        }
        if ($creditCard == '') {
            $msg      = 'error';     
            $cc_error = '*';
        }
        if ($msg == 'error')
            $msg  = 'Please enter required field(s) above!';     
        else
        {
            $msg  = "$firstname $lastname at $address <br/>";
            $msg .= "ordered $flavor2 with $topping2 <br/>";
            $msg .= "using credit card $creditCard <br/>";
        }     
}

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

    if (! $flavor) $flavor = Array();   //to get rid of warning in case $flavor does not exist   
?>                

    <form method=POST action=<?php echo $_SERVER[PHP_SELF] ?> >                  <!-- itself -->
    <fieldset style='width:570px; height:310px; border-color:gold'>
    <legend>Enter Fields Below</legend>

    <table bgcolor=eeeeee>
    <tr>
        <td><b>First Name 
        <td><input type=text name=firstname value='<?php echo $firstname ?>' >
    <tr>
        <td><b>Last Name <font color=red><?php echo $name_error ?></font>
        <td><input type=text name=lastname value='<?php echo $lastname?>' >
    <tr>
        <td><b>Enter Address <font color=red><?php echo $addr_error ?> </font>          
        <td><textarea name=address rows=4 cols=47><?php echo $address ?></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'        <?php if (in_array('vanilla',       $flavor))  echo 'SELECTED'?> > Vanilla</option>        
                <option value='chocolate'      <?php if (in_array('chocolate',     $flavor))  echo 'SELECTED'?> > Chocolate</option>      
                <option value='strawberry'     <?php if (in_array('strawberry',    $flavor))  echo 'SELECTED'?> > Strawberry</option>
                <option value='butter-pecan'   <?php if (in_array('butter-pecan',  $flavor))  echo 'SELECTED'?> > Butter Pecan</option>
                <option value='rocky-road'     <?php if (in_array('rocky-road',    $flavor))  echo 'SELECTED'?> > Rocky Road</option> 
                <option value='french-vanilla' <?php if (in_array('french-vanilla',$flavor))  echo 'SELECTED'?> > French Vanilla</option>
                <option value='pistachio'      <?php if (in_array('pistachio',     $flavor))  echo 'SELECTED'?> > Pistachio</option>
            </select>

    <tr>
        <td><b>Select Topping <font color=red><?php echo $top_error ?> </font>
        <td>
        <input type='checkbox' name='topping[]' value='hotFudge'     <?php if (strpos($topping2,'hotFudge')    !==false) echo 'CHECKED' ?> /> Hot Fudge
        <input type='checkbox' name='topping[]' value='sprinkles'    <?php if (strpos($topping2,'sprinkles')   !==false) echo 'CHECKED' ?> /> Sprinkles
        <input type='checkbox' name='topping[]' value='nuts'         <?php if (strpos($topping2,'nuts')        !==false) echo 'CHECKED' ?> /> Nuts
        <input type='checkbox' name='topping[]' value='whippedCream' <?php if (strpos($topping2,'whippedCream')!==false) echo 'CHECKED' ?> /> Whipped Cream
    <tr>
        <td><b>Choose Credit Card <font color=red> <?php echo $cc_error?> </font>
        <td>
        <input type=radio name=creditCard value='visa' <?php if ($creditCard == 'visa') echo 'CHECKED' ?> /> Visa
        <input type=radio name=creditCard value='mc'   <?php if ($creditCard == 'mc')   echo 'CHECKED' ?> /> Master Card
        <input type=radio name=creditCard value='amex' <?php if ($creditCard == 'amex') echo 'CHECKED' ?> /> Amex
    <tr>
    <td width=155>
    <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>