[ Previous ] [ Contents ] [ Index ] [ Next ]

Example 3b: pagetcl/genstory

The following example script implements the same story generating function of genstory (the previous example) but is implemented as a page Tcl script instead of a library Tcl script. Note that the associated HTML file (genstory.htm) is also included after the Tcl script.

This example can be found in the examples/tcl/pagetcl directory.

    
    # Example 3b: Form generation and handling
    #
    # This operation generates a story based on the
    # form data submitted from the form genstory.htm.
    #
    # Things to notice:
    #
    # * This file should be stored with the HTML pages
    #   of the server.  When a client requests the URL corresponding
    #   to the file, the AOLserver sets the "conn" variable and
    #   evaluates the Tcl.
    #
    # * An error status (500) is returned if the client doesn't
    #   doesn't pass in any form data.
    #
    # * Form data is stored in an ns_set, and accessed
    #   like any other set (e.g., header data).
    #
    # * A counter is used to loop through all the key
    #   value pairs in the form.
    
    set formdata [ns_conn form $conn]
    
    if {$formdata == ""} {
        ns_return $conn 200 text/plain "Need form data!"
        return
    }
    
    # Build up a human-readable representation of the form data.
    
    set hrformdata "<dl>"
    set size [ns_set size $formdata]
    for {set i 0} {$i < $size} {incr i} {
        append hrformdata "<dt>[ns_set key $formdata $i]</dt>\
    	    <dd>[ns_set value $formdata $i]</dd>"
    }
    append hrformdata "</dl>"
    
    ns_return $conn 200 text/html \
    "<HTML>
    <HEAD>
    <TITLE>The story of [ns_set get $formdata name1] and
    [ns_set get $formdata name2]</TITLE>
    </HEAD>
    <BODY>
    <H1>
    The story of [ns_set get $formdata name1] and
    [ns_set get $formdata name2]
    </H1>
    <P>Once upon a time [ns_set get $formdata name1] and
    [ns_set get $formdata name2] went for a 
    walk in the woods looking for a [ns_set get $formdata noun1].
    [ns_set get $formdata name1] was 
    feeling [ns_set get $formdata adjective1] because
    [ns_set get $formdata name2] was so
    [ns_set get $formdata adjective2].  So
    [ns_set get $formdata name1] decided to
    [ns_set get $formdata verb1] [ns_set get $formdata name2]
    with a [ns_set get $formdata noun2].  This made
    [ns_set get $formdata name2] [ns_set get $formdata verb2]
    [ns_set get $formdata name1].
    <P><CENTER>The End</CENTER>
    The form data that made this possible:<BR>
    $hrformdata
    </BODY></HTML>"
    

Here's the associated HTML file:

    
    <HTML>
    <HEAD>
    <TITLE>Automatic Story Generator</TITLE>
    </HEAD>
    <BODY>
    <H1>
    Automatic Story Generator
    </H1>
    <FORM ACTION=genstory.tcl METHOD=POST>
    Noun: <INPUT TYPE=text NAME=noun1><BR>
    Noun: <INPUT TYPE=text NAME=noun2><BR>
    Name: <INPUT TYPE=text NAME=name1><BR>
    Name: <INPUT TYPE=text NAME=name2><BR>
    Adjective: <INPUT TYPE=text NAME=adjective1><BR>
    Adjective: <INPUT TYPE=text NAME=adjective2><BR>
    Verb: <INPUT TYPE=text NAME=verb1><BR>
    Verb: <INPUT TYPE=text NAME=verb2><BR>
    <P><INPUT TYPE=submit VALUE="Generate">
    </FORM>
    <P>
    </BODY></HTML>

Top of Page

[ Previous ] [ Contents ] [ Index ] [ Next ]
Copyright © 1998-99 America Online, Inc.