Gossamer Forum
Home : General : Perl Programming :

CGI Perl calling itself, failed query string

Quote Reply
CGI Perl calling itself, failed query string
The following is called from a simple HTML page using
<A href = "thisProgram.pl?PARA1=value1&PARA2=value2"><?A>


Perl stuff
#!/usr/bin/perl

use CGI qw(:standard);

# This perl script calls itself and posts variables back to itself.
# It can find the virtual path to itself & query string from the server environment variables
my $thisURLandQuery = $ENV{'SCRIPT_NAME'};
if (length($ENV{'QUERY_STRING'})>0)
{
$thisURLandQuery =$thisURLandQuery.'?'.$ENV{'QUERY_STRING'} ;

}


# the first call of the script from the HTML succeeds
# passing in PARA1 and PARA2
# it then returns empty $parameter1 and $parameter2 when it calls itself . Anyone know why?

my %data;
foreach my $name ( param() )
{
$data{$name} = param($name);
}
my $parameter1=$data{'PARA1'};
my $parameter2=$data{'PARA2'};




#Later-on it calls itself from a form being submitted

print "<form method='post' action=$thisURLandQuery>";

Replacing the part in Italics with the following suceeds:

my $parameter1=&kensQueryParse($ENV{'QUERY_STRING'},'PARA1=');
my $parameter2=&kensQueryParse($ENV{'QUERY_STRING'},'PARA2=');

# where kensQueryParse(inputVariable, paraNameToFind)
# is a home-made query-string value extractor
Quote Reply
Re: [kenshail] CGI Perl calling itself, failed query string In reply to
I'm not really sure of the answer based on the provided code, but one thing that stood out, is why are you using a "home-made" query string parser when you can just use CGI.pm in the way it was intended, eg...

Instead of:

my $parameter1=&kensQueryParse($ENV{'QUERY_STRING'},'PARA1=');

...use:

my $parameter1= param('PARA1');

Last edited by:

Paul: Jun 2, 2003, 4:31 PM
Quote Reply
Re: [kenshail] CGI Perl calling itself, failed query string In reply to
As Paul suggested, why not use CGI.pm?

Code:
#!/usr/bin/perl

use CGI;
$IN = new CGI;

print $IN->header();

print "Value for test => " . $IN->param('test');

Its so simple, as most definatly faster than the routine you are currently using.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] CGI Perl calling itself, failed query string In reply to
I have tried both the above suggested methods and whenever I use param() to parse the query string I get good values for the query-string parameters when the script is called from <A HREF ........> in an HTML page BUT then empty parameters returned when the script calls itself using a <form method='post' action=$thisURLandQuery> .
That is why I wrote a long-winded function to parse $ENV{'QUERY_STRING'} which works every time.

BTW If I use use CGI; alone I get an error Undefined subroutine &main::param called I have to use use CGI qw(:standard); to get any method to work. I have tested in IIS and Apache2, same results.

Here is something to run

First put the following in a web page that is not in the same directory as the perl script:

<A href = "queryStringTest.pl?PathToHTML=/">Click to do some calculations</A>

Replace the / with the relative path from the script back to the web page.

Then click the above link call this rediculously stripped-down version of the script:

#!/usr/bin/perl -w
# Skeleton script for running calculations on a web page and getting the reults on the same page
# as well as the original values that were keyed-in


# Use the cgi unit
use strict;
use CGI qw(:standard);
#use CGI;


my $pathToHTML= param('PathToHTML');

# In this example the query string is used to pass-in the relative (virtual path back to the htm that called it.
# used to ovrecome different directory layouts that are required by different server progs.

# This perl script calls itself and posts the query-string that it recieved the first time back to itself.
my $thisURLandQuery = $ENV{'SCRIPT_NAME'};
if (length($ENV{'QUERY_STRING'})>0)
{
$thisURLandQuery =$thisURLandQuery.'?'.$ENV{'QUERY_STRING'} ; # send back the query string if one exists
}


# Get the values from the form
my $form = new CGI;
my $value1=$form->param('value1');
my $value2=$form->param('value2');


&header;

&page1;

#some rediculously simple examples
my $product=$value1*$value2;
my $sum=$value1+$value2;
print "The product is $product, the sum is $sum <br>";

&goBack;

# you will see this value disappear after pressing calcuate
print "<br>for testing, the relative path back to HTML =$pathToHTML will be blank after pressing calculate<br>";


print "</b>";
print "</body>\n";


sub header #send an html header
{

# Send the HTML form header
# Output the correct header to make output HTML format
print "Content-type: text/html\n\n";

}


sub page1
{

print
"
<html>
<head><TITLE></TITLE>
</head>
<body leftmargin='10' marginwidth='10' bgcolor='#FFFFFF'>
<font face='Verdana,Arial,Helvetica,sans-serif'>

<form method='post' action=$thisURLandQuery>

<TABLE BORDER=2 CELLPADDING=1 CELLSPACING=0>

<TR>
<TD>First value
<TD><INPUT NAME='value1' TYPE='text' SIZE='20' VALUE=$value1><BR></TD>
</TR>
<TR>
<TD>Second value
<TD><INPUT NAME='value2' TYPE='text' SIZE='20' VALUE=$value2><BR></TD>
</TR>
</TABLE>
<INPUT TYPE='submit' VALUE='CALCULATE'>
";
}


sub goBack
{
my $localPath=$pathToHTML.'queryStringTest.htm';
print
"
<br><br>
<A href = '$localPath'>Return to selection page</A>

";

}






Quote Reply
Re: [kenshail] CGI Perl calling itself, failed query string In reply to
You can't put a query string inside the action="" part of a form tag. You need to pass in hidden form fields to get the values.
Quote Reply
Re: [Paul] CGI Perl calling itself, failed query string In reply to
Thanks for that.

But it works when I use my long-winded method to parse $ENV{'QUERY_STRING'}

If I were to use a hidden method in the form the parameters would be passed by method post and not get back in to the query string. The script would then not be do the same thing when it calls itself. Or Have I missed something?
Quote Reply
Re: [kenshail] CGI Perl calling itself, failed query string In reply to
P.S. Or do I have to use method GET for all my parameters. I.E. the ones passed initially from the HTML & the ones that I am sending back to the script that were entered in the form the first time? I guess that would allow me to use a method hidden.

It doesnt however solve the mystery why I can parse $ENV{'QUERY_STRING'} that theoretically should not be there!
Quote Reply
Re: [kenshail] CGI Perl calling itself, failed query string In reply to
TESTS CONCLUDED

Results: It was possible to pass a query string that was recieved by the script back to itself using method = GET. This was done by making the recieved query-string a hidden type in the form.

Conclusion: You can't put a query string inside the action="" part of a form tag and expect the CGI param method to recover it. But you can recover it if you parse $ENV{'QUERY_STRING'}