Next / Previous / Contents / TCC Help System / NM Tech homepage

7.16. generateForm(): Build the form element

For a normal chapter page, this function builds the HTML form element and everything underneath it. For the HTML to be generated, refer to Section 7, “reader.cgi: An example script with cookies”.

reader.cgi
# - - -   g e n e r a t e F o r m

def generateForm ( body, userRecord ):
    '''Generate the form element and its content.

      [ (body is an et.Element) and
        (userRecord is a UserRecord instance) ->
          body  +:=  a form element displaying the chapter as
                     specified by userRecord ]
    '''

The form element has two attributes: method='get' and an action attribute whose value is the URL where reader.cgi lives; see Section 7.6.24, “BASE_URL: The URL of the script's directory”.

reader.cgi
    #-- 1 --
    # [ body  +:=  a new form element with method='get' and
    #       action=(BASE_URL)+'reader.cgi'
    #   form  :=  that new element ]
    form  =  et.SubElement ( body, 'form', method='get',
        action=(BASE_URL+'reader.cgi') )

The navigation buttons are added next; see Section 7.17, “navButtons: Add navigational buttons”.

reader.cgi
    #-- 2 --
    # [ form  +:=  next/previous chapter buttons ]
    navButtons ( form )

Here is the simulated chapter content.

reader.cgi
    #-- 3 --
    # [ form  +:=  a div containing the simulated content for
    #       chapter (userRecord.chapterNo) ]
    contentDiv  =  et.SubElement ( form, 'div' )
    contentDiv.text  =  'This is chapter %d.' % userRecord.chapterNo

Next is a horizontal rule and a duplicate set of navigation buttons.

reader.cgi
    #-- 4 --
    # [ form  +:=  (horizontal rule) + (next/previous chapter
    #              buttons) ]
    et.SubElement ( form, 'hr' )
    navButtons ( form )

For the logic that adds the radiobutton group, see Section 7.18, “radioGroup(): Add the radiobutton group”. This function needs the userRecord so that it can set the radiobuttons the way the user last set them.

reader.cgi
    #-- 5 --
    # [ form  +:=  the radiobutton group in the same state as
    #              userRecord.persist ]
    radioGroup ( form, userRecord )

Last on the page is the Forget me button.

reader.cgi
    #-- 6 --
    # [ form  +:=  a submit button with name=FORGET_CONTROL
    #              and value=FORGET_LABEL ]
    div  =  et.SubElement ( form, 'div' )
    forgetButton  =  et.SubElement ( div, 'input', type='submit',
        name=FORGET_CONTROL, value=FORGET_LABEL )