We help IT Professionals succeed at work.

passing parameters using "post" and  CGI object

PeteK asked
I have a CGI object that can access a lot of parameters.

Problem: I want to open a web page and pass the parameters accessed by the CGI object to the web page (using POST and not GET).

How can I pass the parameters in the CGI object to a web page that I want to open?

Watch Question

When generating the form of the new web page, insert the hidden fields into the form.
Here's an example:
Say you have an original web page like this:
<FORM Action="http://yoursite.com/yourscript.cgi">
<input type=text name="ID" value="MyID"><br>
<input name=submit type=submit>

When you click submit button your parameter ID will be passed to the Script and you retrieve it with
$PARAMETER = param("ID");

Now you want this $PARAMETER to pass to another web page and you generate a new web page with the form in it.

print $CGI -> header;
print $CGI -> start_html;
print "<FORM Action=\"http://yoursite.com/yourscript.cgi\">
print "<input type=\"hidden\" name=\"ID\" value=\"$PARAMETER\">";
print "<input name=\"submit\" type=\"submit\">";
print "</FORM>";
print $CGI -> end_html;

This is what I usually do. it is simple and very configurable.
You surely could use the $CGI -> object instead of just printing the form lines.


Are u talking about posting some data to a url(script) from within a script?

Please provide more details.



I am trying to solve a different problem -I'm sorry I wasn't clear.

>Are u talking about posting some data to a url(script) from within a script?


I have a webpage that posts data to a script using a submit button and some fields of a form.  The script (ScriptA) does some processing  and then calls another script (ScriptB) that creates a webpage.  

I want to pass data from ScriptA (which has a CGI object) to ScriptB using POST (because ScriptB expects POSTed parameters).

Desired Sequence:

1)Webpage POSTs to scriptA url :  www.someurl.com/cgi/ScriptA

2) ScriptA process the data and (somehow) POSTs data from its CGI object to ScriptB:

3) ScriptB creates a  webpage from the data posted from script A.

I don't understand to complete step 2.  I can pass data from ScriptA to ScriptB using GET by redirecting to the appropriate  url with the desired parameter appended (www.someurl.com/cgi/ScriptB?Param1=1&Param2=2) but I don't know how to post the data.

How can I do this?


Here is what u can do to call scriptB from scriptA :

use strict;
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
$ua->agent('MyAgentName/1.0 '.$ua->agent);
my $req = new HTTP::Request POST=>'http://www.someurl.com/cgi/ScriptB';
my $res = $ua->request($req);
if($res->is_success) {
     print $res->content;
else {
     print "An Error Occured!\n"

The $res->content above will give the returned html code from scriptB

Hope this helps. Let me know :-)



Thanks a lot.  Sorry for the delay (I had some trouble accessing this site).