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

Hi perl monks, I am very new to CGI scripting, and stuck up here. I am using an HTML to POST some data, and analyzing it at CGI script. With the output I get after analysis, I need to open another cgi at a different server. This is what I have tried.
print $q->start_form( -name => 'main_form2', -method => 'POST', -action => 'http://xxx.com/cgi-bin/perforce?&Submit=describe&changenu +m=$1', -target => '_blank', ); print $q->submit( -name => 'submit_form2', -value => 'See the change', ); print $q->end_form;
I have two problems here. 1) I cannot pass a variable. $1 won't work 2) If I pass it hardcoded, it would work, but the variables after 'perforce?' are not considered, until I refresh the newly opened page. This is my first CGI script,and need your help badly. Thanking in advance, Manoj

Replies are listed 'Best First'.
Re: How to open an URL with variable in CGI?
by 1nickt (Canon) on Jul 27, 2015 at 20:52 UTC

    Remove the first & from the query string; that character separates key=value pairs and you have nothing in front of it.

    Your $1 is not being evaluated because it is inside single quotes.

    Also, where do you set $1? It's usually set by Perl as a regex capture...

    Update: got back to PC; improved formatting

    The way forward always starts with a minimal test.
Re: How to open an URL with variable in CGI?
by tangent (Parson) on Jul 27, 2015 at 20:53 UTC
    I assume that $1 is the result of a regular expression match. If not you should change it to a textual name. The reason the target url is not recognising the query parameters is that this is a POST request. You need to add some hidden fields to pass those parameters:
    my $changenum = $1; print $q->start_form( -name => 'main_form2', -method => 'POST', -action => 'http://xxx.com/cgi-bin/perforce', -target => '_blank', ); print $q->hidden( -name => 'Submit', -value => 'describe', ); print $q->hidden( -name => 'changenum', -value => $changenum, ); print $q->submit( -name => 'submit_form2', -value => 'See the change', ); print $q->end_form;
Re: How to open an URL with variable in CGI?
by marinersk (Priest) on Jul 27, 2015 at 23:26 UTC

    I'm guessing (because it doesn't look like you posted a fully functional example), but -- if you intend $1to be the first parameter sent to the subroutine which contains the code you posted, you need to grab that in advance:

    sub doMyThing { my ($changeNumber, @otherParameters) = @_; : : print $q->start_form( -name => 'main_form2', -method => 'GET', -action => "http://xxx.com/cgi-bin/perforce?Submit=describe\& +changenum=$changeNumber", -target => '_blank', ); : : return; }