http://www.perlmonks.org?node_id=1028981


in reply to Perl CGI redirect

This script gives the user a selection which is more secure than a text box. If the URL shown is what you want then remove the hash from #Search and it should redirect.
#!/usr/bin/perl # n.cgi use strict; use CGI qw( :standard ); use CGI::Carp qw( fatalsToBrowser ); use Date::Calc qw(Add_Delta_Days); # calc dates my ($d,$m,$y) = (localtime)[3,4,5]; $y+=1900; $m+=1; my ($y0,$m0,$d0) = Add_Delta_Days($y,$m,$d,-7); my $FM = sprintf "%02d-%02d-%04d",$m0,$d0,$y0; my $TM = sprintf "%02d-%02d-%04d",$m,$d,$y; # page layout my $page =<< "EndOfText"; <h2>Add Logical:</h2> <form name="form" method="post" action="n.cgi"> <table> <tr> <td align="right"><strong>Logical:</strong></td> <td> <select name="server"> <option value="">select</option> <option value="server1">server1</option> <option value="server2">server2</option> <option value="server3">server3</option> <option value="server4">server4</option> </select> </td> </tr> <tr> <td colspan="2"> &nbsp; </td> </tr> <tr> <td> &nbsp; </td> <td> <input type="submit" name="action" value="Search" /> </td> </tr> </table> </form> EndOfText # get parameters my $q = CGI->new; my $action = $q->param('action'); my $server = $q->param('server'); # construct URL my $URL = join ";","http://a.com:8080/bms/healthMonitor.do?method=redi +rect", "operation=BackupHistorySearch", "subOperation=GetReport", "clientName=$server", "fromDateStr=$FM", "toDateStr=$TM"; # redirect if required if ($action eq '#Search'){ # remove hash to redirect print $q->redirect($URL); } else { print $q->header, $q->start_html('Page Title'), $page, # hash out next 2 lines or remove if OK $q->p("Vars action=$action : server=$server : FM=$FM : TM=$TM "), $q->a({-href=>$URL, -target=>'_blank'},$URL), $q->end_html; }
poj