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

I changed this perl program to have dynamic dropdowns instead of static. It's working, except I need the other form data to be saved whilst they're messin with the drop downs. I'm using HTML::Template. I won't include the template files as they're just basic HTML with TMPL_VAR's. There's 3 template files. 1 is the form, the 2nd for email, 3rd is the confirm screen. So, i was thinking of somehow combining my formparams sub and my getBody sub. I know this could be cleaned up a lot, but i'd like to just getting it working first. UPDATE: I could just manually add something like this to my forparams:
$formparams{FIRSTNAME} = $query->param("firstname");
my $query = new CGI; my %db_hash; my $s_dept = $query->param("s_dept"); my $dept_name=&build_list("D", "",$s_dept); my @dept_loop = @tmp_hash; my @branch_loop; my $branch_name; if ($s_dept = $query->param("s_dept")) { my $s_branch = $query->param("s_branch"); $branch_name=&build_list("B", "AND PARENT_ID = $s_dept",$s_branch); @branch_loop = @tmp_hash; } my @div_loop; my $div_name; if ($s_branch = $query->param("s_branch")) { if ($s_branch != "Choose Branch") { my $s_div = $query->param("s_div"); $div_name=&build_list( "V", "AND PARENT_ID = $s_branch",$s_div) +; @div_loop = @tmp_hash; } } if ($final_sub = $query->param("final_sub")) { # submit and proces +s the form (including send email) &processForm(); exit; } &formparams(); &createForm($formTemplate); exit; #=================================================== # build drop downs #=================================================== sub build_list () { my ($org_type,$sql_insert,$selected)=@_; #receive variable int +o subroutine my %isSelect; $isSelect{$selected}="SELECTED"; my $dbh=DBI->connect('DBI:Oracle:METAD1', 'ei_arf_webform_select' +, 'metad1') or die "Cannot connect to DB: " . DBI->errstr; my $sql = "SELECT org_id,org_name FROM mv_org_structure_list WHERE ORG_TYPE='$org_type' $sql_insert ORDER BY org_na +me ASC"; my $sth = $dbh->prepare($sql) || print "Cannot prepare"; $sth->execute || print "Cannot execute"; my @result; %db_hash = (); while (@result = $sth->fetchrow_array()) { $db_hash {$result[0]} = $result[1]; } @tmp_hash = (); foreach my $key (keys %db_hash) { push @tmp_hash, {ORG_ID => $key, ORG_NAME => $db_hash{$key}, O +RG_SELECT => $isSelect{$key}}; } return $db_hash{$selected}; } #=================================================== # forparams #=================================================== sub formparams () { %formparams = (); $formparams{LOGO} = "/images/logobw.gif"; $formparams{TITLE} = "ARF Exempt Request (Network and/or E-Mai +l Account, Voicemail, Telephone Re-assignment)"; $formparams{PATH} = $ENV{SCRIPT_NAME}; $formparams{DEPARTMENT} = \@dept_loop; $formparams{BRANCH} = \@branch_loop; $formparams{DIVISION} = \@div_loop; } #===================================== # create new form from html template #===================================== sub createForm { $formTemplate = new HTML::Template( filename => $FORM_TMPL_FILE ); for my $key (keys %formparams) { $formTemplate->param($key => $formparams{"$key"}); } my ($formTemplate) = @_; print $formTemplate->output; return; } #===================================== # process submit request #===================================== sub processForm { my $body; $body = getbody(); my $email_subject = "EDS/Telecom Action"; &sendMail ($smtp, $to, $cc, $from, $email_subject, $body); print "<br>"; print "Thank you! <br/>"; print "<br>"; print "Your request has been submitted.<br><br>"; print "<a href=\"javascript:window.close();\">Close Window</a>\n"; return; } #================================================== # get email body set for specific body requirement #================================================== sub getbody() { my $resulttemplate = new HTML::Template( filename => $RESULTS_TMPL +_FILE ); my $displaytemplate = new HTML::Template( filename => $DISPLAY_TMP +L_FILE ); my $params = getdata(); $resulttemplate->param(FIRSTNAME => $params->{'firstname'}); $resulttemplate->param(LASTNAME => $params->{'lastname'}); #bunch of other parmas removed, you get the idea # To return a list of parameters in the template of $displaytemplate ( +keys) my @parameter_names = $displaytemplate->param(); foreach my $name (@parameter_names) { # Assign the value of $resulttemplate to the key of $displayte +mplate # Since the keys have the same name in both templates. $displaytemplate->param($name => $resulttemplate->param($name)) +; } $body = $resulttemplate->output; if ($body) { print $displaytemplate->output; # Print out form data to s +creen return $body; # Returns form data to +be sent by email } else { print "Error: Cannot send email body to send<br>"; exit; } } #======================================= # Get CGI data from the form submitting #======================================= sub getdata() { my %params; my $query = new CGI; foreach my $key ($query->param) { $params{$key} = $query->param($key); } return \%params; } #===================================== sub sendMail { # SEND EMAIL... BLAH BLAH return; }

Replies are listed 'Best First'.
Re: save form data/combine subs
by agianni (Hermit) on Mar 28, 2007 at 15:05 UTC
    Please consider asking a specific questions and posting some generalized code that allows us to help you without having reading 445 lines of code. If you feel you need to post that much code, please consider using <readmore> tags. Please read How do I post a question effectively.
    perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
      sorry, i shortened it up to relevant code. I figured people would want to see it all. Sometimes I put to little and you guys ask to see more. I've decided to just add to my formparams to save the form data when the page submits for the dynamic drop downs. like so
      $formparams{LASTNAME} = $query->param("lastname");