Can I put Multiple handlers in a CGI script?

Sivakatirswami katir at hindu.org
Wed Aug 28 01:13:01 EDT 2002


on 08-27-2002 12:35 PM, Rich Mooney at tech at paynesparkman.com wrote:

> As I understand it, a CGI needs to be in the following format:


Maybe this will help you get started... This CGI drives the "shop site"
dynamic order generation at

www.himalayanacademy.com/hawaii/iraivan/donate/

I am not an expert, so the code might be much more refined... But it works.
Credit for inspiration goes to Andu Novac

We can develop these things on my own machine now with MAX OSX... Which is
really great!
=======

#!/mc  ## this path will vary  depending on your host...but you can install
MC on a virtual host without root access... No problem.

on startup
if $REQUEST_METHOD is "POST" then
  read from stdin until empty
 
  ## put the form data into an array, sort & save off
  put  urlDecode (it)  into tDataIn
  split tDataIn by "&" and "="
  
  
  if "quantity" is among the lines of keys(tDataIn) then
   
    ## this is the initial selection of product/quantity choices
    ## so we process the quantity to build the order form.
   
 set the numberformat to "#.00"
  
  ## calculate price by product
  
 
  
   switch tDataIn["00_item_name"]
   case "2003_calendar"
       put calculateCalendar (tDataIn["quantity"]) into tsubtotal
       put "2003 Calendar" into  theItem
       break
   case "box_5_rudrakshas"
       put calculateRudraksha (tDataIn["quantity"]) into tsubtotal
       put "Boxes of Five Rudraksha Beads" into  theItem
       break
   end switch
  
  ## coerce shipping integer to dollar format with fake calculation
 put 0 into tShipping
  put (0+tShipping) into tShipping
   
   put tsubtotal+tshipping into tTotal
   
   
   ## generate order form
  put url "file:../hawaii/iraivan/donate/order_form_template.html" into
tOrderForm

replace "###item###"  with theItem in tOrderForm
 replace "###Quantity###"  with tDataIn["quantity"] in tOrderForm
replace "###item_name###" with tDataIn["00_item_name"] in tOrderForm

  replace "###Subtotal###"  with tSubtotal in tOrderForm
  replace "###Shipping###"  with tShipping in tOrderForm
 replace "###Total###"  with tTotal  in tOrderForm
  
  put  tOrderForm into tResponse
 
  ## return online-invoice-order  to client to fill in and exit

  put "Content-Type: text/html" & cr
   put "Content-Length:" && the length of tResponse & cr & cr
   put tResponse
   exit startup
  end if
  
  
  # If this is not the entry page submission,
  # then it must be the actual order form submission.
  
  
  if "01_Quantity" is among the lines of keys(tDataIn) then
  
  ##Yes, this is the actual order coming down the pipe
  ## Build the submission data first
  
  ## First get full product title out
  switch tDataIn["00_item_name"]
     case "2003_calendar"
           put "2003 Iraivan Temple Calendar" into  tDataIn["00_item_name"]
           break
     case "box_5_rudrakshas"
          put "Boxes of Five Rudraksha Beads" into  tDataIn["00_item_name"]
          break
   end switch
 put keys(tDataIn) into tFields
  sort lines of tFields
     repeat for each line x in tFields
        put (x & tab & tDataIn[x] & cr ) after tSubmit
     end repeat
     --delete last line of tSubmit
         # Now check for missing fields
     set the itemdel to tab
     put empty into missing_fields
     repeat for each line x in tSubmit
     if (item 1 of x is not among the words of "09_Phone 13_add2 14_add3
19_comments" ) then
           if item 2 of x is empty then put item 1 of x & "<br>" after
missing_fields
      end if
     end repeat
     if missing_fields is not empty then
         sendUserFeedback (missing_fields)
     exit startup
     end if
     
     # Acknowledge the order to the user:
     
  put  tDataIn["10_First"] & " " & tDataIn["11_Last"] into vName
 put url "file:../hawaii/iraivan/donate/order_confirmation.html" into
tResponse
replace "###NAME###" with vName in tResponse
replace "###item_name###" with tDataIn["00_item_name"] in tResponse

end if

  # save the order to file on server
    
put cr&cr&"=========="&cr& the date & "  " & the time & cr& tSubmit after
url "file:../formdata/temple_donations.txt"

# return  order acknowledgement page to client

  put "Content-Type: text/html" & cr
   put "Content-Length:" && the length of tResponse & cr & cr
   put tResponse
   
   
# Now email the appropriate notices

    put "/usr/lib/sendmail -t" into mprocess
 
  # Order alert notice to headquarters
    
    put "An order for " & tDataIn["01_Quantity"] & " of "  &
tDataIn["00_item_name"] & "has arrived from " & vName & " of " &
tDataIn["15_City"] & ". Who added this comment: "  & tDataIn["19_comments"]
& cr & cr & ". Go get the CC#." into tMessage
     open process mprocess for write
     write "From:" && "webmaster at hindu.org" & cr to process mprocess
     write "To:" && "iraivan at hindu.org" & cr to process mprocess
     write "Subject:" &&tDataIn["00_item_name"] & "  Order" & cr & cr to
process mprocess
    write tMessage & cr to process mprocess
    
        close process mprocess
   wait until the openprocesses is empty
   
    # email Confirmation to the buyer
     open process mprocess for write
        write "From:" && "webmaster at hindu.org" & cr to process mprocess
     write "To:" && tDataIn["08_email"] & cr to process mprocess
     put tSubmit into tConfirmText
     delete line 6 to 8 of tConfirmText ## deletes credit card info from the
email before sending
     write "Subject:" && "Your Order for " && DataIn["00_item_name"] & cr &
cr to process mprocess
    write the date& "  " & the time & cr&cr& tConfirmText  & cr & cr& "The
above order has been placed. Your credit card information is not displayed
and is secure. Please keep this email until your order arrives. If you do
not get your items within 30 days, please email iraivan at hindu.org with a
copy of this order. Thank you."  to process mprocess
    close process mprocess
   wait until the openprocesses is empty

 end if
end startup

function calculateCalendar x
 switch  x
case 1
  put "17.00" into tSubTotal
  break
case 2
  put "32.00" into tSubTotal
  break 
case 3
  put "47.00" into tSubTotal
  break
case 4
  put "62.00" into tSubTotal
  break 
case 5
  put "77.00" into tSubTotal
  break
end switch
  return tSubTotal

end calculateCalendar

function calculateRudraksha x
 switch  x
case 1
  put "20.00" into tSubTotal
  break
case 2
  put "40.00" into tSubTotal
  break 
case 3
  put "60.00" into tSubTotal
  break
case 4
  put "80.00" into tSubTotal
  break 
end switch
  return tSubTotal

end calculateRudraksha

on sendUserFeedback missing_fields
 put "Sorry, please fill in the following required fields, and submit again.
<BR>Do not click the BACK button in your browser. " into tResponse
 put "<BR><A HREF=" & quote &
"http://www.himalayanacademy.com/hawaii/iraivan/donate/" &quote& "> Please
click this link instead to order again.</A>" & "<p>" after  tResponse
put missing_fields after tResponse
 put "Content-Type: text/html" & cr
  put "Content-Length:" && the length of tResponse & cr & cr
  put tResponse
end sendUserFeedback

 
> #! mc
> on Startup
>   #script
> end Startup
> 
> I also have the impression that any script in this CGI had to be between "on
> Startup" and "end Startup".
> So does this mean that one CGI file is one handler and that I can't pass
> arguments to it or can I do something like this:

Om shanti,
Hinduism Today

Sivakatirswami
Editor's Assistant/Production Manager
katir at hindu.org 
www.HinduismToday.com, www.HimalayanAcademy.com,
www.Gurudeva.org, www.hindu.org

Read The Master Course Lesson of the Day at
http://www.gurudeva.org/lesson.shtml




More information about the metacard mailing list