CGI - Processing web forms
See also: Creating and Processing Web Forms with CGI (Tutorial)
Create the following form1.html file on your www folder:
<HTML>
<TITLE>Simple Web Form</TITLE>
<BODY>
<b>Simple Web Form</b><p>
<FORM ACTION="formhandler.cgi">
<INPUT TYPE="TEXT" NAME="Field" SIZE="25"><BR>
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>
Now create and save in the same folder the formhandler.cgi script:
#!www/rebol.exe -c
Rebol []
print [<HTML><PRE> mold system/options/cgi </HTML>]
When you write "My Name" in the field and press the Submit button, your form1.html will call formhandler.cgi, and this script will print what the CGI protocol passes to Rebol and is stored in system/options/cgi which is:
make object! [
server-software: "Cheyenne/1.0"
server-name: "Ungaretti"
gateway-interface: "CGI/1.1"
server-protocol: "HTTP/1.1"
server-port: "80"
request-method: "GET"
path-info: "/formhandler.cgi"
path-translated: "www\formhandler.cgi"
script-name: "/formhandler.cgi"
query-string: "Field=My+Name&Submit=Submit"
remote-host: none
remote-addr: "127.0.0.1"
auth-type: none
remote-user: none
remote-ident: none
Content-Type: none
content-length: "0"
other-headers: ["HTTP_ACCEPT" {text/html,application/xhtml+xml,application/...
]
This is good to know, but Rebol offers a function to decode the CGI, named decode-cgi that converts the raw form data into a REBOL block that contains words followed by their values. The information we want (the contents of the field), are in the query-string variable. So change formhandler.cgi script as follows:
#!www/rebol.exe -c
Rebol []
print [<HTML><PRE> decode-cgi system/options/cgi/query-string </HTML>]
The browser output now is:
Field My Name Submit Submit
CGI cool example
This is the CGI version of the RSP's cool example. Save it as coolexample.cgi in Cheyenne's www folder. Open in browser using localhost/coolexample.cgi.
#!www/rebol.exe -c
Rebol []
; First, a not very elegant way of avoiding crashes:
either system/options/cgi/query-string = none [
system/options/cgi/query-string: ""
decoded: ""
][
decoded: second decode-cgi system/options/cgi/query-string
]
; Lets show what's in "decoded":
print {<font face="courier">}
print "decoded = " probe decoded print "<br>"
; Here we start HTML
print {
<HTML>
<br><br>
<TITLE>Cool Example</TITLE>
<BODY>
<b>Cool Example</b><p>
<FORM ACTION="coolexample.cgi">}
print {<INPUT TYPE="SUBMIT" NAME="Triangle" VALUE="Triangle"><br><br>}
if decoded = "Triangle" [
print {<svg width="120" height="120">
<polygon points="0,100 50,0 100,100"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" />
</svg> <br>}
]
print {<INPUT TYPE="SUBMIT" NAME="Square" VALUE="Square"><br><br>}
if decoded = "Square" [
print {<svg width="120" height="120">
<rect width="100" height="100" style="fill:rgb(0,0,255);stroke-width:10;stroke:rgb(0,0,0)" />
</svg> <br>}
]
print {<INPUT TYPE="SUBMIT" NAME="Circle" VALUE="Circle"><br><br>}
if decoded = "Circle" [
print {<svg width="120" height="120">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg> <br>}
]
; Now we finish HTML
print {
</FORM>
</BODY>
</HTML>}