home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / delphcgi.zip / CGI.ZIP / SAMPLES / FORM / PIZZFORM.PAS < prev   
Pascal/Delphi Source File  |  1995-07-27  |  5KB  |  164 lines

  1. unit PizzForm;
  2.  
  3. {  Application: PIZZAD.DPR
  4.          Author: Michael B. Klein <mbk@baldrick.com>
  5.            Date: May 16, 1995
  6.  
  7.     Sample CGI back-end using the TCGI component.  Based on the Visual Basic
  8.     sample CGI app PIZZA.BAS, which is supplied with Windows httpd.
  9.  
  10.     Thanks to Robert B. Denny for Windows httpd and Windows CGI, and for his help
  11.     getting me around a pretty sticky applcation design issue.  }
  12.  
  13. interface
  14.  
  15. uses
  16.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  17.   Forms, Dialogs, CGI, CGIDlg;
  18.  
  19. type
  20.   TMainForm = class(TForm)
  21.      CGI: TCGI;
  22.      CGIDlg: TCGIDlg;
  23.      procedure SendOrderForm;
  24.      procedure ProcessOrder;
  25.      procedure FormCreate(Sender: TObject);
  26.   private
  27.      { Private declarations }
  28.      procedure SendHeader(const Title: String);
  29.      procedure SendFooter;
  30.      procedure RejectOrder(const Reason: String);
  31.   public
  32.      { Public declarations }
  33.   end;
  34.  
  35. const
  36.     crlf: String = #13#10;
  37.  
  38. var
  39.   MainForm: TMainForm;
  40.  
  41. implementation
  42.  
  43. {$R *.DFM}
  44.  
  45. procedure TMainForm.SendOrderForm;
  46. begin
  47.     with CGI do begin
  48.         SendHeader('Pizza Order Form');
  49.         Send('<FORM METHOD="POST" ACTION="/cgi-win/pizzad.exe">');
  50.         Send('<B>Godzilla''s Pizza -- Internet Delivery Service</B><P>');
  51.         Send('At present, we accept internet orders only for medium (13") size pizzas.');
  52.         Send('It is now ' + FormatDateTime('h:mm AM/PM', Now) +'. ');
  53.         Send('You will have your pizza by ' + FormatDateTime('h:mm AM/PM', Now + EncodeTime(0,45,0,0)) + '.');
  54.         Send('<PRE>');
  55.         Send('          Name: <INPUT SIZE=30 NAME="name">'+crlf);
  56.         Send('Street address: <INPUT SIZE=30 NAME="address">'+crlf);
  57.         Send('  Phone number: <INPUT SIZE=15 NAME="phone">'+crlf);
  58.         Send('          City: <SELECT NAME="city">');
  59.         Send('                <OPTION SELECTED>Pasadena (free)');
  60.         Send('                <OPTION>Altadena (free)');
  61.         Send('                <OPTION>So. Pasadena ($1.00)');
  62.         Send('                <OPTION>Arcadia ($1.00)');
  63.         Send('                <OPTION>Monrovia ($2.50)');
  64.         Send('                </SELECT>'+crlf);
  65.         Send('</PRE>');
  66.         Send('Which toppings would you like? <BR>');
  67.         Send('<OL>');
  68.         Send('<LI> <INPUT TYPE="checkbox" NAME="topping" VALUE="pepperoni"> Pepperoni.');
  69.         Send('<LI> <INPUT TYPE="checkbox" NAME="topping" VALUE="sausage"> Sausage.');
  70.         Send('<LI> <INPUT TYPE="checkbox" NAME="topping" VALUE="anchovies"> Anchovies.');
  71.         Send('</OL>');
  72.         Send('To order your pizza, press this button: <INPUT TYPE="submit" VALUE="Order Pizza">.');
  73.         Send('</FORM>');
  74.     end;
  75.     SendFooter;
  76. end;
  77.  
  78. procedure TMainForm.ProcessOrder;
  79. var
  80.     Cust,
  81.    Address,
  82.     City,
  83.     Phone,
  84.     Toppings: String;
  85.     i: Char;
  86. begin
  87.     with CGI.FormFields do begin
  88.         Cust := Values['name'];
  89.         Address := Values['address'];
  90.         Phone := Values['phone'];
  91.         City := Values['city'];
  92.         Toppings := Values['topping'];
  93.  
  94.         i := '1';
  95.         while IndexOfKey('topping_'+i) > -1 do begin
  96.             Toppings := Toppings + ', ' + Values['topping_'+i];
  97.             Inc(i);
  98.         end;
  99.     end;
  100.  
  101.     if (Cust = '') or (Address = '') or (Phone = '') or (City = '') then
  102.         RejectOrder('you didn''t fill in all of the fields')
  103.     else begin
  104.         SendHeader('Order Confirmation');
  105.         with CGI do begin
  106.             Send('<H1>Order Confirmation</H1>');
  107.             Send('Your order has been received, and appears valid. Here it is:');
  108.             Send('<PRE>');
  109.             Send('          Name: ' + Cust+crlf);
  110.             Send('Street address: ' + Address+crlf);
  111.             Send('  Phone number: ' + Phone+crlf);
  112.             Send('          City: ' + City+crlf);
  113.             Send('      Toppings: ' + Toppings+crlf);
  114.             Send('</PRE>');
  115.             Send('If you have any corrections, please call 555-555-5555 immediately!');
  116.         end;
  117.         SendFooter;
  118.     end;
  119. end;
  120.  
  121. procedure TMainForm.SendHeader(const Title: String);
  122. begin
  123.     with CGI do begin
  124.         Send('<HTML><HEAD><TITLE>' + Title + '</TITLE></HEAD>');
  125.         Send('<BODY>You''re using '+Profile.ExtraHeaders.Values['User-Agent']+'!<P>');
  126.     end;
  127. end;
  128.  
  129. procedure TMainForm.SendFooter;
  130. begin
  131.     with CGI do begin
  132.         Send('<HR>');
  133.         Send('Click below to send mail to our order desk:<BR>');
  134.         Send('<A HREF="mailto:Orders@Godzilla.com">');
  135.         Send('<ADDRESS><Orders@Godzilla.com></ADDRESS></A>');
  136.         Send('</BODY></HTML>');
  137.     end;
  138. end;
  139.  
  140. procedure TMainForm.RejectOrder(const Reason: String);
  141. begin
  142.     SendHeader('Order Rejected');
  143.     with CGI do begin
  144.        ServerStatus := stBadRequest;
  145.         Send('<H1>Can''t process your order</H1>');
  146.         Send('We can''t process your order because ' + Reason + '. ');
  147.         Send('Please correct your order and re-send it.');
  148.     end;
  149.     SendFooter;
  150. end;
  151.  
  152. procedure TMainForm.FormCreate(Sender: TObject);
  153. begin
  154.     case CGI.Method of
  155.         rmGet: SendOrderForm;
  156.         rmPost: ProcessOrder;
  157.     end;
  158.   if CGI.Profile.DebugMode then CGIDlg.Execute;
  159.   CGI.SendContent;
  160.   Application.Terminate;
  161. end;
  162.  
  163. end.
  164.