To build this sample:
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.
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.cgiMain :- cgi(request_method, RM), processMethod(RM). cgiMain :- throw($cgiMain failed\n$).
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.% % 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 '<-' button on your browser to change your form and resubmit it.$), sendFooter.
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, !.
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:
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.