Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Perl CGI redirect

by poj (Priest)
on Apr 16, 2013 at 18:11 UTC ( #1028981=note: print w/ replies, xml ) Need Help??


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


Comment on Re: Perl CGI redirect
Download Code
Replies are listed 'Best First'.
Re^2: Perl CGI redirect
by Anonymous Monk on Apr 16, 2013 at 20:58 UTC

    Thanks.. However I need to input the server name(s) to search..

      In that case just replace the select
      <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>
      with a text box
      <textarea name="server" rows="1" cols="30" wrap="virtual"></textarea>
      poj

      Thanks for the help... Changed it to add text box. However this is opening in the same page. Is is possible to open on a new page and also check for mutiple servers

      #!/usr/bin/perl # n.cgi use strict; use CGI qw( :standard ); use CGI::Carp qw( fatalsToBrowser ); use POSIX qw(strftime); my $today = time; my $yesterday = $today - 60 * 168 * 60; my $FM = strftime "%m-%d-%Y", ( localtime($yesterday) ); my $TM = strftime '%m-%d-%Y', localtime; # page layout my $page =<< "EndOfText"; <h2>NBU Backup Report</h2> <form name="form" method="post" action="n.cgi"> <table> <tr> <td align="right"><strong>Hostname</strong></td> <TD> <TEXTAREA NAME="server" ROWS=1 COLS=30 WRAP="server"></TEXTAREA> </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 = "http://a.com:8080/bms/healthMonitor.do?method=redirect&oper +ation=BackupHistorySearch&subOperation=GetReport&clientName=$server&f +romDateStr=$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), }
        Not sure about multiple servers but to open a new page you can do it with some javascript. I have added the javascript to a new clickme button as an example.
        #!/usr/bin/perl # n.cgi use strict; use CGI qw( :standard ); use CGI::Carp qw( fatalsToBrowser ); use POSIX qw(strftime); my $today = time; my $yesterday = $today - 60 * 168 * 60; my $FM = strftime "%m-%d-%Y", ( localtime($yesterday) ); my $TM = strftime '%m-%d-%Y', localtime; # 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> <textarea id="ta" name="server" rows="1" cols="30" wrap="soft"></te +xtarea> </td> </tr> <tr> <td colspan="2"> &nbsp; </td> </tr> <tr> <td> <button type="button" onClick="javascript:clickme()">Click Me</ +button></td> <td> <input type="submit" name="action" value="Search" /> </td> </tr> </table> </form> EndOfText # construct URL my $URL = 'http://a.com:8080/bms/healthMonitor.do?method=redirect' .'&operation=BackupHistorySearch' .'&subOperation=GetReport' ."&fromDateStr=$FM&toDateStr=$TM"; # javascript my $JSCRIPT=<<EOF; function clickme(){ var URL = \'$URL\' + '&clientName=' + document.getElementById("ta").value // comment out if or remove when working OK if (confirm(URL)==true) { window.open(URL) }; } EOF # get parameters my $q = CGI->new; my $action = $q->param('action'); my $server = $q->param('server'); # add server $URL .= "&clientName=$server"; # redirect if required if ($action eq 'Search'){ # remove hash to redirect print $q->redirect($URL); } else { print $q->header, $q->start_html( -title => "Some Title", -script=> $JSCRIPT), $page, $q->end_html; }
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1028981]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2015-07-07 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls