Let us now return to a more complex form example. We previously looked at the Python Quiz form.
The HTML to produce this is:
<H1>Python Quiz: </H1> <form method = "post" action = "simple-form.cgi"> What is thy name: <input name="name"><P> What is thy quest: <input name="quest"><P> What is thy favorite color: <select name="color"> <option selected>chartreuse <option>azure <option>puce <option>cornflower <option>olive draub <option>gunmetal <option>indigo2 <option>blanched almond <option>flesh <option>ochre <option>opal <option>amber <option>mustard </select> <P> What is the weight of a swallow: <input type="radio" name="swallow" value="african" checked> African Swallow or <input type="radio" name="swallow" value="continental"> Continental Swallow <P> What do you have to say for yourself <textarea name="text" rows=5 cols=60></textarea> <P> Press <input type="submit" value="here"> to submit your query. </form>
Note: that the Method attribute is set to POST. This desirable since many name/value pairs are sent to the CGI script.
If the method was set to GET instead the call would look something like this:
http://dave.cs.cf.ac.uk/simple-form.cgi?name=Dave& quest=Find+Holy+Grail& color=olive+draub& swallow=continental& text=I+am+Tired
Also:
The Post method sends all this dat via standard input which is far neater.
This has several input name/value pairs.
To extract out the relevant value for a given name is straightforward though:
&ReadParse(*input)
$input{'name'}
It is also useful to process the form input for a Textarea field so that line breaks are inserted - since HTML does not preserve linebreaks and carriage returns will be present in the multiline output.
The Perl commands:
($text = $input{'text'}) =~ s/\n/\n<BR>/g;does this.
This Perl is a little complex for complete study now. Essentially the following occurs
($text = $input{'text'})
\n
characters are substituted (s/.../ command)
by \n<BR>
.
s/\n/\n<BR>/g
The s/old_sting/new_string/ command is commonly used in Perl. The g at the end of the command indicates a global substitution (all instances get replaced) rather than the (default) first found.
So our complete Perl script is as follows:
The complete Perl code is as follows:
#!/usr/local/bin/perl # Copyright (C) 1994 Steven E. Brenner # This is a small demonstration script # to demonstrate the use of # the cgi-lib.pl library require "cgi-lib.pl"; # Read in all the variables from form &ReadParse(*input); # Print the header print &PrintHeader; print &HtmlTop ("cgi-lib.pl demo form output"); # Do some processing, and print some output # add <BR>'s after carriage returns # to multline input, since HTML does not # preserve line breaks ($text = $input{'text'}) =~ s/\n/\n<BR>/g; print << ENDOFTEXT; You, $input{'name'}, whose favorite color is $input{'color'} are on a quest which is $input{'quest'}, and are looking for the air speed/velocity of an $input{'swallow'} swallow. And this is what you have to say for yourself:<P> $text<P> ENDOFTEXT # If you want, just print out a list of all of the variables. print "<HR>And here is a list of the variables you entered...<P>"; print &PrintVariables(*input); # Close the document cleanly. print &HtmlBot;