Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

form to cgi to cgi

by Anonymous Monk
on Jan 26, 2008 at 02:17 UTC ( #664414=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I hope someone can help me i need to post a form to a cgi that then posts the data to another cgi.

Replies are listed 'Best First'.
Re: form to cgi to cgi
by gam3 (Curate) on Jan 26, 2008 at 03:21 UTC
    There are lots of ways to do this, but here is a start
    #!/usr/bin/perl use strict; use CGI; my $cgi = CGI->new(); print $cgi->header(); print <<EOP; <html> <head> <title>test</title> </head> <body> <div> <h1>Please input data below</h1> <form> Name: <input name="testdata" text="name"/> <form> </div> </body> </html> EOP use Data::Dumper; use HTML::Form; use LWP::UserAgent; if (my $data = $cgi->param('testdata')) { my $ua = LWP::UserAgent->new; print "got: " . $data; print "<br/>"; my $html = "http://localhost/cgi-bin/test"; # http://localhost/y.cgi is where the socond form is submitted. my $form = HTML::Form->parse(<<HTML, 'http://localhost/'); <form action="/y.cgi"> <intput type="text name="test" /> </form> HTML $form->attr( 'test', $data); my $response = $ua->request($form->click); print Dumper $response; print "<br/>"; } 1;
    I hope there are enough hints here to get you going.

    You might also just want to get the form from the page then fill it out. UPDATE: Took chromatics suggestion and now use header().

    -- gam3
    A picture is worth a thousand words, but takes 200K.
      print "content-type: text/html\n\n";

      Most web browsers will probably accept that, but it's invalid HTTP. If you're going to use CGI anyway, why not:

      print $cgi->header();

        Then it's fortunate his script is communicating using CGI (not HTTP) with a web server (not a browser).

        While the header CGI is defined as being an HTTP header, web servers have a long history of accepting "\n" as a CGI header line seperator. The CGI spec even mentions it allows the CGI header to be seperated from the CGI body by LF or by CRLF. It's up to the server to construct a valid HTTP response from that de-facto valid CGI request.

        Or if you're referring to the unusual text case, CGI/HTTP field names are case-insensitive (RFC2616 S4.2). Incidentally, so are media types and subtypes (RFC2616 S3.7).

        While using print $cgi->header(); is a great idea seeing as CGI is already in play, what his script does is by no means invalid.

      Thank You
Re: form to cgi to cgi
by ww (Archbishop) on Jan 26, 2008 at 02:44 UTC
    1. Read How do I post a question effectively?
    2. use "search" to find the many times this topic has been addressed.
    3. Recognize that you will usuallly get excellent and very prompt help here when you show that you have made some effort to code an answer to your problem, and put your question in the form of a well-formatted, brief statement of the code, the problem and the error messages, if any.
    4. Understand that the Monastery is an institution of learning; not a source of free work.

    Suggestion: register as a Monk so that you can edit ill-considered posts like your OP.

Re: form to cgi to cgi
by olus (Curate) on Jan 26, 2008 at 02:38 UTC
    You'll have to make yourself clearer.
    We need a better description to your problem in order to be able to assist you.
    Please read How (Not) To Ask A Question for some guidance.

    And why do you need to post from one cgi to the other?
      Sorry let me try this again at stage one i submit a form First name Last name ect. that posts to ord.cgi that works all find and dandy but now my father is holding me hostage until i can figure out how to catch the form in another cgi that sends it to ord.cgi but ive been looking for several days through manuals and cant find the necessary commands to have the first cgi catch the form? thanks for any help.
        Did your father specify that you send it back to the *SAME* script as receives the first (for further processing, to capture additional data, ???), as your parent post seems to say?

        Eminently doable, but if you're new to Perl, you may find it easier to write two scripts.

        Script 1, ord.cgi (according to one reading of your post) will need to be enhanced to catch First name Last Name, etc. and to pass those on to script 2, perhaps (if you're going to do this with an intermediate page requesting additional information) as elements -- possibly hidden -- of a new webpage to be created by script 2. Then, extend script 2 so as to catch both the previously captured elements -- the names, etc -- and to pass the aggregate on to either yet-another-new-page or yet another script.

        On the other hand, the final part of your question (after your assertion that you've been searching you say "cant find the necessary commands to have the first cgi catch the form" (sic) ) seems to say that you have not yet figured out how to have script 1 catch elements of your form... so I may be way off track on what you're looking for.

        So please. Think your problem through, and then, post again. And not to be snarky about it (yes, I make typos, misspellings and careless errors, too), please write using well-formed (eg, not run-on) sentences and paragraphs that structure your questions for the reader in a clear and coherent manner with correct spelling and punctuation.

        NB: FWIW, the parent of this node, and this node, were posted after my next one was posted.

        The first cgi catches the form if the form is submited to it.

        Ask your father what is the purpose of this exercise.
Re: form to cgi to cgi
by bradcathey (Prior) on Jan 26, 2008 at 15:27 UTC

    I'd use my favorite module HTML::Template. Untested:

    HTML for form1.cgi:

    <form action="" method="post"> <input type="text" name="firstname" /> <input type="text" name="lastname" /> <input type="submit" /> </form>


    #!/usr/bin/perl use strict; use CGI qw /:cgi/; use HTML::Template; my $query = new CGI; $template = HTML::Template->new( filename => 'form2.tmpl' ); $template -> param( firstname => $query->param('firstname'), lastname => $query->param('lastname') ); print $template->output;

    HTML form2.tmpl:

    <form action="" method="post"> <input type="text" name="firstname" value="<tmpl_var firstname>" /> <input type="text" name="lastname" value="<tmpl_var lastname>" /> <input type="submit" /> </form>

    And if you are using selects, checkboxes, or radio buttons, HTML::FillInForm is great. Hope your father approves.

    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://664414]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-05-26 17:15 GMT
Find Nodes?
    Voting Booth?