Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Passing CGI form variables back to same page for further updates to same page

by Lori713 (Pilgrim)
on Jan 31, 2008 at 15:38 UTC ( [id://665370]=perlquestion: print w/replies, xml ) Need Help??

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

Howdy, One of the reports I'm creating needs to allow the user to select some options, and then display another set of options based on the original selections. For example, if you select Funding Group A, then only Programs 1, 2 and 3 are shown; if you select Funding Group B, then only Programs 7, 8 and 9 are shown.

The values in the Program box are dynamic; they come from the database. Therefore, once the user selects a Funding Group, I need to go back to the database to narrow down what options they can select for Program.

The trouble I'm having is getting the Continue button to pass its value back to the Main Menu. I use CGI.pm and check to see if the value of the Continue button has been passed back so I know how to proceed. For some reason (and I cringe to think how obvious it will probably be), I cannot get the flippin' thing to pass the value of the Continue button back so I know which items to update.

(In the future, I will be wanting to make the form 'sticky' but that's another post if I can't figure it out later... right now I just want to know if I'm refreshing the page, or starting from scratch. Later, I will be running the actual report once I'm satisfied they've selected what they need to).

Below is my .pl file (taken down to its basics for this post with readmore tags to shorten it):

#!/usr/local/bin/perl5_8 use strict; use HTML::Template; use DBI; use CGI ':standard'; my $CGI = CGI->new; # Clear buffers and set up web page (required) $|=1; print $CGI->header; # Get the main menu, and pass $CGI variables to the template as needed my $template = HTML::Template->new(filename => 'xethel.tmpl', associate => $CGI, loop_context_vars => 1, global_vars => 1, die_on_bad_params => 0 ); ############################ Begin Section ########################### +##### # Fetch CGI params. my $continue = $CGI->param('bmscont'); print "bmscont $continue<br>"; my $selected_dpta = $CGI->param('dpta2'); print "dpta $selected_dpta<b +r>"; my $selected_fund = $CGI->param('fund2'); print "fund $selected_fund<b +r>"; my $selected_prog = $CGI->param('prog2'); print "prog $selected_prog<b +r>"; my $dbh=DBI->connect("dbi:Oracle:".$databs,$userid,$passwd) || die "co +nn die"; # Funding Group Selection my $fund_sql = " SELECT A.FIELDVALUE, A.XLATLONGNAME FROM XLATTABLE_VW A WHERE A.FIELDNAME = 'NC_FUND_GROUPS' AND A.EFFDT = (SELECT MAX(EFFDT) FROM XLATTABLE_VW A2 WHERE A.FIELDNAME = A2.FIELDNAME) AND A.EFF_STATUS = 'A' ORDER BY A.XLATLONGNAME "; my $sth= $dbh->prepare($fund_sql) || die "prep fund die"; $sth->execute || die "exec fund die"; my @fund_loop = (); my ($fund, $fund_descr, %fund_hash); # Fetch the remaining items for the list from the database while ( my $dat = $sth->fetch ) { my %fund_hash; $fund = $dat->[0]; $fund_descr = $dat->[1]; $fund_hash{fund} = $fund; $fund_hash{fund_descr} = $fund_descr; push(@fund_loop, \%fund_hash); }; # Find variables entered on original main menu and use in subsequent d +atabase # calls to populate the remaining boxes. if ( $selected_dpta eq "" || $selected_dpta == "" ) { $selected_dpta = 2 } if ( $continue eq "Continue" ) { # hard-coded for now until we figure out how to fetch selected + fund $selected_fund = "AA"; } else { # hmmmmmmmm print "BUZZZZZZZZZ wrong answer... try again"; } # Program list based on the Funding Group selection above my $where_prog = "WHERE A.NC_FUND_GROUPS = ('$selected_fund')"; my $prog_sql = " SELECT DISTINCT PROGRAM_CODE, DESCR FROM PS_NC_BMS_PROGCODE A $where_prog ORDER BY DESCR "; $sth= $dbh->prepare($prog_sql) || die "prep prog die"; $sth->execute || die "exec prog die"; my @prog_loop = (); my ($prog, $prog_descr, %prog_hash); # Fetch the remaining items for the list from the database while ( my $dat = $sth->fetch ) { my %prog_hash; $prog = $dat->[0]; $prog_descr = $dat->[1]; $prog_hash{prog} = $prog; $prog_hash{prog_descr} = $prog . ' - ' . $prog_descr; push(@prog_loop, \%prog_hash); }; $sth->finish(); $dbh->disconnect; #++++++++++++++++++++++++++++ End Section ++++++++++++++++++++++++++++ ++++++ ############################ Begin Section ########################### +##### # Pass params and print page $template->param( fund_loop => \@fund_loop, prog_loop => \@prog_loop, selected_dpta => $selected_dpta, selected_fund => $selected_fund, selected_prog => $selected_prog, ); print $template->output(); # The Main Menu request method is normally "get". When a customer tim +es # out due to inactivity, a message will appear telling them they timed + out # and will send them back to the Main Menu. If the Request Method isn +'t # set to POST right before the Main Menu is displayed, the customers w +ill # be put into an endless loop for timing out. See the subroutine "tim +eout". $ENV{"REQUEST_METHOD"} = "post";


And this is my HTML:Template file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html><head><title>WRS Main Menu</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1 +" /> <meta name="description" content="My Main Menu" /> <style type="text/css"> body { font-family:Arial; color:black; font-size:small; } form { margin: 0em; } hr { width:100%; color:#D3D3D3; } .link { white-space:nowrap; color:#B22222; text-align:left; } .section { font-weight:bold; color:#B22222; background:#DCDCDC; text-align:center; } .hide { white-space:nowrap; display:none; } .bold { white-space:nowrap; font-weight:bold; } .norm { white-space:nowrap; } .vert { vertical-align:top; } </style> </head> <body> <form name="form_main" method="post" action="ncw_summ_rpt14.pl"> <input type="hidden" name="selected_dpta" value="<TMPL_VAR selected_dp +ta>" /> <input type="hidden" name="selected_fund" value="<TMPL_VAR selected_fu +nd>" /> <input type="hidden" name="selected_prog" value="<TMPL_VAR selected_pr +og>" /> <table class="bold" width="100%" border="0" summary=""> <tr><td class="section" colspan="3">Step 1: Select Report / Link</td>< +/tr> <tr><td style="text-align:center; text-decoration:underline;">Reports< +/td></tr> <tr><td> <input type="radio" name="rpt_id" value="14_summary" id="rpt_id14" style="cursor:pointer;" checked="checked" onclick="action='ncw_summ_rpt14.pl';" /> <label for="rpt_id14">14 - BMS by Account Detail</label> </td></tr> </table> <table class="bold" width="100%" border="0" summary=""> <tr><td class="section" colspan="4">Step 2: Select / Enter Criteria</t +d></tr> <tr id="rpt14_row1" name="rpt14_row1"> <td class="vert" width="25%"> <label for="fund2" name="fund1" id="fund1">Funding Group:</label></b +r> <select name="fund2" id="fund2" multiple size="5"> <option value="xxx" selected="selected">-- Select Funding Group --</ +option> <option value="ALL">ALL</option><TMPL_LOOP fund_loop> <option value="<TMPL_VAR fund>"><TMPL_VAR fund_descr></option></TMPL +_LOOP> </select></td> <td class="vert" width="25%"> <label for="dpta2" name="dpta1" id="dpta1">Department Breakdown:</la +bel></br> <select name="dpta2" id="dpta2" size="3"> <option value="2">Select Department Breakdown</option> <option value="4" selected="selected">4 Digit</option> <option value="6">6 Digit</option> </select></td> <td class="vert" width="25%"> <a href="www.msn.com" id="bmshelp" name="bmshelp" target="_blank" style="color:#B22222;">Chartfield Criteria Menu Help</a></br></br +></br> <input type="button" name="bmscont" id="bmscont" value="Continue" onclick="hideShowContinue('14_summary');"/> <input type="reset" name="reset" id="reset" value="Reset" /> </td> </tr> <tr id="rpt14_row2" name="rpt14_row2"> <td><label for="prog2" name="prog1" id="prog1">Program Code:</label></ +br> <select name="prog2" id="prog2" multiple size="5"> <option value="ALL" selected="selected">ALL</option><TMPL_LOOP prog_ +loop> <option value="<TMPL_VAR prog>"><TMPL_VAR prog_descr></option></TMPL +_LOOP> </select></td> </tr> <tr><td colspan="5">&nbsp;</td></tr> <tr> <td class="vert" style="text-align:center;"> <input type="submit" name="submitForm" id="submitForm" value="View Report" style="width:12em;" onclick="return validate();" /></td> </tr> </table> </form> <script type="text/javascript"> <!-- function hideShowContinue(value) // This function is invoked after a customer clicks on the Continue bu +tton. { var rptid2 = value; var myOption = "default"; if ( rptid2 == "14_summary" && document.form_main.fund2.selectedIndex + == 0 ) { alert('You must select a Funding Group to proceed.'); myOption = "Missing"; } if ( rptid2 == "14_summary" && myOption != "Missing" ) { location.href = "xethel.pl"; } } function validate() // This function changes the "View Report" button to say "Processing"; + for // Report 14, it will make sure they selected something from the Depar +tment // box before proceeding. { getRadios = document.getElementsByTagName("input"); for ( x = 0; x < getRadios.length; x++ ) { if ( getRadios[x].type == "radio" && getRadios[x].checked ) { radioSelected = getRadios[x].value; } } mydptachoice = document.form_main.dpta2.selectedIndex; if ( radioSelected == "14_summary" && mydptachoice == 0 ) { alert('You must select a Department Breakdown before proceed +ing.'); return false; } else { document.form_main.submitForm.value = "Processing..."; return true; } } // --> </script> </body></html>


So far, I searched and searched on here to find a solution/another way, and I was VERY excited about CGI::Application until I checked our servers and found the module is not loaded. (It takes a freakin' act of Congress to get anything loaded so I'm trying for solutions based on what I have available to me).

Am I in the right ballpark? Please feel free to point me in another direction, etc., if I'm running down the wrong path on this one. It just seems like it should be so simple but I'm not seeing the forest for the trees today (and all this week from the look of things).

Thanks!

Lori

Replies are listed 'Best First'.
Re: Passing CGI form variables back to same page for further updates to same page
by arkturuz (Curate) on Jan 31, 2008 at 15:55 UTC
    The values in the Program box are dynamic; they come from the database. Therefore, once the user selects a Funding Group, I need to go back to the database to narrow down what options they can select for Program.

    Maybe it would be better (if the corresponding database query is not too expensive) to generate the whole options thing from database, and narrow options per item via JavaScript. It would very simplify your data processing job on the server-side program, as you just have to process submitted data, and not decide whether to display more options to the user, which one and when. It seems more complicated.

    Also, for the more complicated form processing jobs, maybe you should consider using a powerful CGI::FormBuilder module.

    As of CGI::Application, you can always install it in local directory under your file-system privileges.

      Thank you for your suggestion. I'm now looking into populating the select boxes with all options, and then using Javascript to remove the items I don't want. I am currently tripping over IE's refusal to recognize certain javascript actions on their select boxes.

      Unfortunately, Javascript is NOT my strong suit and I've posted some questions on some Javascript forums to see if I can get some help in that area.
Re: Passing CGI form variables back to same page for further updates to same page
by olus (Curate) on Jan 31, 2008 at 17:45 UTC

    This reply is just for post reference. Lori713 got his answers on the CB

    The issue here is that the form never gets submited to the script that reads the info from the database. <input type="button" ... > does not submit the form, and the javascript handler isn't doing it either.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://665370]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-24 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found