djbryson has asked for the wisdom of the Perl Monks concerning the following question:

ok, so I have a perl form. It reads in an HTML template file. Right now i'm populating a drop down like so
HTML template:
<select name=s_dept onchange="sub_form('dept')"> <option>Choose Dept</option> <TMPL_LOOP DEPT_LOOP> <option><TMPL_VAR DEPT></option> </TMPL_LOOP> </select>

And then in my perl file I query a database, ... put that into an array. then
my @dept_loop; foreach my $dept (@dept) { push(@dept_loop, {DEPT => $dept}); %formparams = ( DEPT_LOOP => \@dept_loop, );<br> # there are other elements i left out that are irrelvant.

then create my form after that... Anyway, that's all working fine. I populate the drop downs fine. But now I want to read 2 fields in from the database into a hash i suppose(ID and NAME). Populate the <option> tag with the NAME and the value="" parm with the ID. So, whatever they click I have the ID, submit the form. Then do a query to build the 2nd drop down. Select NAME2, ID2 from X where id = $id; Sound good? I'm just not sure what approach to take. I don't think I can use the TMPL_LOOP anymore. Maybe I can do everything in the perl file and just stick variables for the dropdowns in the HTML template. Some notes. I have to read in the template. I can't change it to be all the same file. I'd like to, cause it would be easier for me, but can't. I'm a perl newb, so keep that in mind too.

Replies are listed 'Best First'.
Re: some advice...
by friedo (Prior) on Mar 23, 2007 at 04:29 UTC

    You should be able to do what you want by just using more variables in your template:

    <select name="s_dept" onchange="sub_form('dept')"> <option>Choose Dept</option> <TMPL_LOOP DEPT_LOOP> <option value="<TMPL_VAR ID>"><TMPL_VAR NAME></option> </TMPL_LOOP> </select>

    (Naturally you have to make sure both ID and NAME are in the hash that you push onto DEPT_LOOP.)

    You could also use a TMPL_IF conditional to control display of the second menu. If the second menu array is there, it will show, but if it's empty, the conditional will fail.

      so populate the template vars like so:
      push @dept_loop, {ORG_ID => $key, ORG_NAME => $hasho{$key}};
      Thanks man, that worked!!
Re: some advice...
by f00li5h (Chaplain) on Mar 23, 2007 at 12:32 UTC

    CGI::FormBuilder is an answer to your problem, but not to your question.

    You still need to feed it the hashref of value => label, but it will do all the icky HTML whatnot for you.

    HTH

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;