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

Dear Monastic Masters ...

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:&nbsp;<input type="text" name="e +mail" size="30" value="[% email %]"><br><br><br> Password:&nbsp;<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>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<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

      Thank you. I was not aware of url_param. That definitely helps.

      However, after updating my code, I'm still seeing some strangeness (I'm probably missing something).

      My user arrives at the form by following an email link. The link is basically:

      my $forward_url = "$base_url/cgi-bin/login.cgi?uid=$uid&aflag=$aflag&s +flag=$sflag";

      When the user clicks on the link in their email, they are taken to login.cgi and the name-value pairs are grabbed by url_param. Thanks again. Now, the first time a user enters the page, the form is rendered blank by the template. I'm now sending thsi to the template:

      if ($cgi -> url_param('first_launch') ne 'false') { my $requid = $cgi -> url_param('uid'); my $aflag = $cgi -> url_param('aflag'); my $sflag = $cgi -> url_param('sflag'); my $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; };

      And here is the template:

      <tr class="row_config_white"> <td colspan="2" align="center" height="200"> <form name="login_form" method="post" action="[% base_ +url %]/cgi-bin/login_fastapprove.cgi?first_launch=false"><br> <h2>Windows into Waukesha Login - FASTapprove! +</h2><br> <font color="red">[% error_message %]</font><b +r><br> Email Address:&nbsp;<input type="text" name="e +mail" size="30" value="[% email %]"><br><br><br> Password:&nbsp;<input type="password" name="pa +ssword" size="20"><br><br><br> <!--<input type="hidden" name="first_launch" v +alue="false">--> <input type="hidden" name="requid" value="[% r +equid %]"> <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"> requid = [% requid %], aflag = [% aflag %], sflag = [% + sflag %], first_launch = [% first_launch %]<br> <a href="[% base_url %]/cgi-bin/forgot_pw.cgi">Forgot +Password</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="[% base_url + %]/cgi-bin/request_account.cgi">Create New User account</a> </td> </tr>

      I'm using first_launch to keep track of state. Obviously, the user should only visit the empty form once. Notice that I've commented out the hidden input for first_launch above. I moved first_launch to the query string and set it equal to false. This works fine.

      No matter what I do, however, I'm unable to pass first_launch as hidden. If I pass it as hidden, and change the script in the CGI from url_param to param, it isn't recognized. All of the other hidden parameters now work fine.

      Why can't I pass first_launch the same way (as hidden)??? I am perplexed. I'd rather not have it in the query string.

      Again, thank you so much!

        Why can't I pass first_launch the same way (as hidden)??? I am perplexed. I'd rather not have it in the query string.

        I doubt that you can't, so my advice is 1) write a self contained program to test your theory 2) after disproving your theory (haha :D) figure out your real problem