XML Script logo next up prev
Simple CGI handling
Reading and comparing text data

A simple form to handle data:

Guess the suspect:
which could have the source:

<CENTER >Guess the suspect:
  <FORM action="guess.xst" method="POST" >
    <INPUT type="TEXT" name="guess" maxlength="15">
  </FORM>
</CENTER>

The text is sent as the content of guess tags, within the CGI tags i.e. a guess sub-object of the object CGI. Note that some web-browsers don't like the INPUT tag to be closed, but as this isn't well-formed XML (all tags closed) then whenever you use an XML-based tool (like X-Tract) you need to replace the left-hand "<" with "&lt;". This stops the parsing engines from complaining about badly-formed XML.

<XST >Content-type: text/html

<!-- HTML headers here -->

# \.answer := "Verbal" #

<_if test="# \CGI\guess eq \.answer #" >
  <then ><h2 >Correct!</h2></then>
  <else ><h2 >No! Try again.</h2>

<!-- put the FORM HTML code in here,
     so they can try again -->

  </else>
</_if>

<!-- Don't forget to close the BODY
     and HTML tags you opened in the header -->

</XST>

Handling HIDDEN values

In the above, you might want to keep track of the number of guesses they made. Within the FORM tags, add another INPUT tag. This one won't be seen by the person making the guesses.

<INPUT type="HIDDEN" name="number" value="0">

So rewrite the above template thus:


<XST >

# \CGI\number neq \CGI\number + 1 #
<_if test="# \CGI\number gt 5 #" >
  <then ><h2 >Hard luck! Ran out of guesses.</h2></then>
  <else > <!-- the original _if statement here... -->

<!-- now your FORM HTML. Put an
     interpolation in to increment the "HIDDEN" field e.g.: -->

    <CENTER >Guess the suspect:
      <FORM action="guess.xst" method="POST" >
        <INPUT type="TEXT" name="guess" maxlength="15">
        <INPUT type="HIDDEN" name="number" 
                           value="\# \CGI\number \#">
      </FORM>
    </CENTER>
  </else>
</_if>

<!-- now close your tags, as before -->

</XST>

Number crunching

XML Script defines several functions for handling numeric data: adding, multiplication, modulus, averaging... so you could process any amount of numeric data server-side and then return a file with the results embedded in it. Simple processing is very trivial indeed:

# \CGI\price * (1 + \.VAT ) # returns the price with VAT

# date_parse( \CGI\date ) # parses "date month year" format

# 'Your order will be completed by' # 
  # date_str("%d %M %y", time() + 3600 * 24 * 7) #
    -- next week

More complicated processing can be achieved by sending several CGI name/value pairs, with the same name. If they are all called e.g. "subobject," then the following processing can easily be done by X-Tract:

# average( \CGI\subobject ) #

# 'It took' sum( \CGI\subobject) 'seconds.' #

# count( \CGI\subobject{.__content != ""}) 'non-empty fields' #
Next...

XML Script homepage | Documentation home | XML Script docs | Command list | Function list | X-Tract docs

X-Stream, X-Tract and XML Script are trade marks of DecisionSoft Limited
© Copyright 1998-2000 DecisionSoft Limited