Infoform CGI Sample

Infoform is a simple example of a form editting application.

Building the Sample

To build this sample:

  1. Compile infoform.pro and link it with the libraries acgi and list to create infoform.xpl

The main entry point looks at the CGI variable request_method, which was asserted by the CGI executable shell, and calls processMethod with its value. Usually a 'get' request is used when the user wants to receive a form for the first time, and a 'post' request is used to submit information. You can control which type of request is used by setting 'method=' in your <FORM ...> definition.

cgiMain :-
  cgi(request_method, RM),
  processMethod(RM).
cgiMain :-
  throw($cgiMain failed\n$).
The predicate processMethod/1 calls various helper predicates to check the information on the form and either thanks the user or gives them the opportunity to edit and fix the inputs. It generates HTML for the output form using the extended predicate cgiSend/1.
% 
% For the initial get, simply return our HTML form
%
processMethod('GET') :-
  cgiSend($Content-type: text/html$),
  cgiSend($$),
  cgiSendLocalHTMLFile('infoform.htm').
% 
% After the user has filled in the form, we need to check it, then 
% send a response back. 
% 
processMethod('POST') :- 
  sendHeader,
  checkFacts,
  writeRequestLog,
  cgiSend($Thank you! Your information request has been successfully submitted!$),
  cgiSend($<P>Return to <A HREF="/index.html">Amzi!'s home page</A>.$),
  sendFooter.
processMethod('POST') :-
  cgiSend($<P>Press the 'Back' or '&lt;-' button on your browser to change your form and resubmit it.$),
  sendFooter.
Here is one of the checking predicates. It looks at the various fact/2 clauses that were asserted in the dynamic database by the CGI executable shell. Each fact represents an input field that was filled in on the original HTML form. In this case it sees if the user has requested a catalog or newsletters. If not,then there is no potential problem. If so, then make sure there is information entered for the three address fields.
checkAddress :-
  fact(request, RL),
  not(member(catalog, RL)),
  not(member(newsletters, RL)),
  !.
checkAddress :-
  fact(address1, A1),
  fact(city, C1),
  fact(country, C2),
  !.
checkAddress :-
  cgiSend($Please fill in your mailing address including street, city, region and country.$),
  fail, !.

Running the Sample

Installing CGI programs can be complex, and depends on the web server you are using. Regardless of the web server, the following tasks must be completed:

  1. Copy the CGI script and the executable acgi[.exe] (from amzi/lsapis/cgi) to the directory your web server runs CGI programs from (usually cgi-bin under Unix or Scripts under Windows).
  2. Copy the appropriate infoform_<platform>.htm file to your CGI directory and call it infoform.htm. Make sure the pathname is correct in the <FORM> POST_ACTION.
  3. Rename acgi[.exe] to have the same name as your XPL file. In this case, infoform.exe runs infoform.xpl.
  4. Ensure the process that runs your web server can find the Amzi! Logic Server (amzi.dll or libamzi.so). This might mean putting the library on your path or another environment variable, or copying it to cgi-bin or the /usr/lib directory.
  5. If you are using a .cfg file, ensure the web server process can find it. This might mean putting it in cgi-bin or having the AMZI_DIR environment variable set for the web server process.
  6. Ensure your web server process and read and/or execute access to all of the above files.

To help in debugging CGI scripts, you will want to consult the error log files for your server.

Look in the Hello Sample for details on running CGI programs under various web servers.