Perobl has asked for the wisdom of the Perl Monks concerning the following question:
I am rather new to TT2. I am attempting to pass CGI params back and forth between a template and a CGI script. The script is being used to process the same form (TT) under slightly different conditions. I am passing "flags" (parameters) via CGI.pm from the script and then back via HTML hidden input. The info in the flags is not sensitive.
When a user clicks a link, my CGI is launched (lets call it login.cgi). At this stage, the CGI simply presents an empty form that is generated by a template using TT2. The query string includes my "flag" parameters:
print "Location: http://$base_url/cgi-bin/login.cgi?uid=$uid&aflag=$af +lag&sflag=$sflag\n\n";
This fires the script and a small template file that gets embedded into a larger page via the content directive. This part works great. At this stage, all of my information is getting passed to form perfectly.
Now the user adds some data to the form, and this is where I run into problems. Here is the information that I'm passing the template from the CGI script:
my $vars; my $requid = $cgi -> param('uid'); my $aflag = $cgi -> param('aflag'); my $sflag = $cgi -> param('sflag'); # dynamic page generation (first time entry) if ($cgi -> param('first_launch') ne 'false') { $vars = { base_url => $base_url, requid => $uid, aflag => $aflag, sflag => $sflag, }; my $template = 'login.tt'; print $cgi -> header; $tt -> process($template, $vars) || die $tt -> error(); exit; };
The template looks like this:
<tr class="row_config_white"> <td colspan="2" align="center" height="200"> <form name="login_form" method="post" action="[% base_ +url %]/cgi-bin/login.cgi"><br> <h2>Login</h2><br> <font color="red">[% error_message %]</font><b +r><br> Email Address: <input type="text" name="e +mail" size="30" value="[% email %]"><br><br><br> Password: <input type="password" name="pa +ssword" size="20"><br><br><br> <input type="hidden" name="first_launch" value +="false"> <input type="hidden" name="uid" value="[% uid +%]"> <input type="hidden" name="aflag" value="[% af +lag %]"> <input type="hidden" name="sflag" value="[% sf +lag %]"> <input type="submit" value="Login"><br><br><br +> </form> </td> </tr> <tr class="row_config_white"> <td colspan="2" align="center" height="20"> <a href="[% base_url %]/cgi-bin/forgot_pw.cgi">Forgot +Password</a> | <a href="[% base_url + %]/cgi-bin/request_account.cgi">Create New User account</a> </td> </tr>
At this stage, the user will attempt to enter his/her login. They click submit and the form info is passed back to the same CGI. This time, however, the "first time entry" portion of the script is ignored because the hidden input has set first_launch to false.
If this is bypassed program control goes on its merry way to check the form and then process it.
If I remove sflag from $vars (where it is passed to the template from the CGI), the program behaves. However, the moment I insert it, all of the CGI params are lost and I'm stuck in the entry area of the page forever. I must be doing something wrong with sflag, but I just can't see it. Any help would be greatly appreciated!
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem Passing Param Between Template Toolkit & CGI
by Anonymous Monk on Jun 18, 2010 at 16:26 UTC | |
by Perobl (Beadle) on Jun 18, 2010 at 20:07 UTC | |
by Anonymous Monk on Jun 18, 2010 at 22:42 UTC | |
by Perobl (Beadle) on Jun 19, 2010 at 15:46 UTC |