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

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

print('''                                                
    <html>
    <head>
    <title>Process an HTML form</title>
    </head>
    <body bgcolor=lightyellow>
    <h1><center>The Ice Cream Shop</center></h1>
    <h2>Process HTML form data using custom HTTPparam module</h2>    
''')

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

params = HTTPparam.getGET() or {}                             #access the GET form data as a dict
print('GET params:', params,'<br>')                     #display the returned params as a dict

params = HTTPparam.getPOST() or {}                           #access the POST form data as a dict
print('POST params:', params,'<br>')                    #display the returned params as a dict

params = HTTPparam.getAll() or {}                             #access all form data as a dict
print('All params:', params,'<br>')                     #display the returned params as a dict

param = HTTPparam.getValue('hello') or ''                     #access a single param value
print('Param: hello=', param,'<br>')                    #display the value

param = HTTPparam.getValue('firstname') or ''                 #access a single param value
print('Param: firstname=', param,'<p>')                 #display the value

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

for (name, value) in params.items():
    if type(value) is list:                                 #if it is a list
        value = ', '.join(value)                            #join it into a string , separated
    print(F'<tr><td>{name}</th><th>{value}</td></tr>')

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
#=================================================================================