#!/usr/bin/env python3
#=====================================================================================
# Retrieve all parameters from an HTML form 
#=====================================================================================
import os
import HTTPparam

print("Content-Type: text/html \n")             #required cgi header

print('''                                                
    <html>
    <head>
    <title>Process an HTML form</title>
    </head>
    <body bgcolor=lightyellow>
    <h1><center>The Ice Cream Shop</center></h1>
    <h2>Process data from another page</h2>
''')                                 

method = os.environ.get('REQUEST_METHOD')
print(F"You are using the <b>{method}</b> method <br><br>") 

print('''     
    <table border=2>
    <tr bgcolor=tan>
    <th>Element Name</th><th>Element Value</th>
''') 

firstname  = HTTPparam.getValue('firstname')             #get HTML form field value or None if nothing 
lastname   = HTTPparam.getValue('lastname')                
address    = HTTPparam.getValue('address')
brand      = HTTPparam.getValue('brand')         
flavor     = HTTPparam.getValue('flavor')                #flavor is a list
topping1   = HTTPparam.getValue('topping1')   or ""                
topping2   = HTTPparam.getValue('topping2')   or ""      #if None is returned set it to ""                
topping3   = HTTPparam.getValue('topping3')   or ""                
topping4   = HTTPparam.getValue('topping4')   or ""                
creditCard = HTTPparam.getValue('creditCard') or ""    

if firstname  == None : firstname  = ""                 #another way to clean None         
if lastname   == None : lastname   = ""                 #if nothing is entered you get None
if address    == None : address    = ""                 #if None clear the field         
if brand      == None : brand      = ""         
if flavor     == None : flavor     = ""         
            
flavor2 = ""
if type(flavor) is list :                               #if it is a list
    flavor2 = ','.join(flavor)                          #join it into a string , separated          

print("<tr><th>Firstname</th>   <td>", firstname,  "</td>") 
print("<tr><th>Lastname</th>    <td>", lastname,   "</td>") 
print("<tr><th>Address</th>     <td>", address,    "</td>") 
print("<tr><th>Brand</th>       <td>", brand,      "</td>") 
print("<tr><th>Flavor</th>      <td>", flavor2,    "</td>") 
print("<tr><th>Topping</th>     <td>", topping1,topping2,topping3,topping4, "</td>") 
print("<tr><th>Credit Card</th> <td>", creditCard, "</td>") 

print('''
    </table>
    </body>
    </html>
''')



#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/staff/sultan/public_html/cgi-bin/python')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================