Forms and input

  • NCSA'sFORMS TUTORIAL


  • The general mechanism that I suggest you use for getting input is via the "QUERY_STRING", which we already encountered in one of our CGI examples

  • If, for example, you have a link on your page that invokes a CGI script, you can send data to the script as follows:

    <a href="cgi-bin/feed.cgi?grapes">Hot text</a>
    Click to feed the script grapes
    Click to feed the script raisins
    Click to feed the script garbage


  • The content of the above "feed.cgi" is the following:
    #!/bin/csh

    echo "Content-type: text/plain"
    echo ""
    echo "Thank you for feeding me: "
    echo $QUERY_STRING


    But

    you say, "AnDY, that's STATIC input -- it has to be written into my html document."

    Quite true. It's only the QUERY_STRING mechanism that this demonstrates (although you may find uses for even this simple input mechanism).

  • Complex data entry from forms can also be sent as a QUERY_STRING, and the challenge then becomes, how do I decode the string when it arrives?

    In the simplest case, we will be retrieving only a choice, as indicated by a "radio button":

    <FORM METHOD=GET ACTION="cgi-bin/fooditem.cgi">
    Grapes <INPUT TYPE="radio" VALUE=grapes NAME="foodtype">
    Raisins <INPUT TYPE="radio" VALUE=raisins NAME="foodtype">
    Garbage <INPUT TYPE="radio" VALUE=garbage NAME="foodtype">
    <INPUT TYPE="submit" VALUE="Submit">
    </FORM>

    Grapes Raisins Garbage

    Source for fooditem.cgi


    Simple submissions like the following are also fairly easy to deal with:
    <FORM METHOD=GET ACTION="../../cgi-bin/phonenum.cgi">
    Phone number:
    <INPUT TYPE="text" SIZE=16 NAME=phonenum>
    <INPUT TYPE="submit" VALUE="Submit">
    </FORM>

    Phone number:

    In this case the QUERY_STRING variable is going to contain both the name, phonenum, and the number:

    	QUERY_STRING=phonenum=1234567
    
    So, if we want to keep a list of the phone numbers, we may want to reformat:

    set JUSTNUM = `echo QUERY_STRING | sed s/phonenum=//`

    The above "stream editor" command, added to the phonenum.cgi script will strip off the name of the variable, leaving just the number, which can then be added to a data file:

    cat $JUSTNUM >> /home/chingon/blackbook

    Source for phonenum.cgi


    Source for form.cgi is here

  • You may need to replace the +'s in the string returned by the form, you can do this with the unix 'sed' command
  • This script demonstrates what URL Encoding looks like. Type some symbols like !$#%$%

    <FORM METHOD=GET ACTION="cgi-bin/choice.cgi">
    <SELECT NAME="NUM_MENU">
    <OPTION>ONE
    <OPTION>TWO
    <OPTION>THREE
    <OPTION>FOUR
    </SELECT>
    <INPUT TYPE="submit" VALUE="Submit choice">
    </FORM>

    Source for choice.cgi is here
  • I will cover other ways to parse input, but sed is effective for simpler operations.