[prog] Filling an option list (was: perl woes...)
Wolfgang Petzold
petzold at villa-chaos.de
Mon Jun 2 16:31:33 EST 2003
Hello again!
> If you should ever find that way, Wolfgang, please share! (so would I,
> except I'm not really searching any more...)
Well, actually I have just done it quick-and-dirty for a test. I have to
say, you got me interested therein. Let me just re-phrase it a bit into
some kind of pseudo code for better understanding.
Of course I know these files are only little readable and maintainable; of
course there would be a lot of error detecting and error processing to do.
I just wanted to see how it works... as I just stated, quick and dirty.
I hope you can see the interesting part:
- retrieve your data from the data base
- see how you can stuff that data into the list of hash references
that HTML::Template->param() expects for the TMPL_LOOP tag(s)
Wolfgang
-- template file: optiontest.html.t --
<html>
<head>
<title>option test</title>
</head>
<body>
<h1>Option test</h1>
<form action="<TMPL_VAR ESCAPE=1 NAME="selfurl">" name="form1">
<select name="sel1" <TMPL_VAR NAME="sel1_disabled"> onChange="document.form1.submit();">
<TMPL_LOOP NAME="sel1option">
<option
value="<TMPL_VAR ESCAPE=1 NAME="value">"
<TMPL_VAR NAME="selected">
>
<TMPL_VAR NAME="text">
</option>
</TMPL_LOOP>
</select>
<input type="hidden" name="stage" value="1">
</form>
<TMPL_IF NAME="stage1">
<hr>
<p>Selection 1: <TMPL_VAR NAME="sel1selection">
</TMPL_IF>
</body>
</html>
--------------------------------------
-- script file: optiontest -----------
#!/usr/local/bin/perl -T
use strict;
use warnings;
use CGI;
use HTML::Template;
# Here I have some methods that do the actual data base processing.
# As an example,
# getSomeQueryList()
# which returns a list of hash references, and the keys are the column
# names, say 'id' and 'name'.
#
use MyDataBaseInterfaceModule;
my $cgi = CGI->new();
my $page = HTML::Template->new(filename => "optiontest.html.t");
my $db = MyDataBaseInterfaceModule->new();
# a kind of a main loop:
#
if ($cgi->param("stage") == 1) {
#
# Form 'form1' has been submitted. The user has made the first
# choice.
#
# Set all the template vars so that the page renders as wanted.
#
$page->param(stage1 => "true");
$page->param(sel1selection => $cgi->param("sel1"));
#
# Entries for the option list.
# Because I want to disable the drop-down list now, I don't have
# to put in all the values; of course you can if you like to --
# it will be similar to the 'else'-part. In this case you can
# check each $item (see 'else'-part) for equality with
# $cgi->param("sel1") -- the user's choice -- and set 'selected'
# to "" or "selected".
#
my $selection = $cgi->param("sel1");
$page->param(sel1option => {
value => "",
text => $selection,
selected => "selected"
});
$page->param(sel1_disabled => "disabled");
} else {
#
# The form has not yet been submitted. So, get the appropriate
# values and display them in the option list.
#
$page->param(stage1 => "");
$page->param(sel1_disabled => "");
my @anything = $db->getSomeTableList();
#
# construct that list of hash references that $page->param()
# expects for that TMPL_LOOP tag. First, add a default entry
# and then add the result of the data base request.
#
my @list = ();
push @list, {
value => "",
text => "--please choose--",
selected => "selected",
};
foreach my $item (@list) {
my $id = $item->{'id'};
my $name = $item->{'name'};
push @list, {
value => $id,
text => $name,
selected => "",
};
}
$page->param("sel1option" => \@list);
}
print $page->output();
__END__
------------------------------------
More information about the Programming
mailing list