Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Forwarding Post Data

by Danikar (Novice)
on Nov 29, 2007 at 23:17 UTC ( [id://654004]=perlquestion: print w/replies, xml ) Need Help??

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

So I have a form I am working on, and right now we have it set up so that if a specific field (city) is not filled out it just prints a error with a javascript popup thingy. Well we want to change it so that if that field is not there instead of printing a warning it goes onto the next page and a perl script catches the exception and prints out a list of cities. We are doing this to make is less reliant on Javascript. My plan is to make it so (1) The Javascript no longer checks that box. (easy) (2) Change the action to a script that does nothing but check for this exception. (easy) (3) Have that script print a form with list of cities. (easy) (4) =( If the city is defined, forward all post data to the next script. (hard?) Here is a somewhat pseudocode version of the program. Just need me forward function heh.
if($POST{'city'}) { forward('POST', "script.cgi", #POST); } else { ### Prints Form With List of Cities print $choose_city_form; }

Replies are listed 'Best First'.
Re: Forwarding Post Data
by aquarium (Curate) on Nov 29, 2007 at 23:58 UTC
    i think that you only want to use POST method when (a) you don't want the browser user to see the parameters in the url (b) you're sending a fair amount of data with the url, i.e. more than url length allows.
    btw you can get around the parameter issue (a) by using an iframe.
    anyway...what i'm getting to is that you can simply append your parameters to the url, for the next cgi script to pick up as per usual via param method. e.g.
    $next_url = "http://blabla.com/cgi/script.cgi?"; $next_url .= "city=" . $city if($city); ... $next_url =~ s/\?$//; # remove question mark if no parameters added +to next_url

    funny thing that you're trying to move away from javascript though...as a decent ajax form can check all fields, provide feedback, and post the data, all without a reload...and form reloads are continuity disruptive and a general pain for users to use.
    generally people are moving to ajax rather than away from it...as it has markedly improved the user experience
    the hardest line to type correctly is: stty erase ^H
Re: Forwarding Post Data
by thezip (Vicar) on Nov 30, 2007 at 00:54 UTC

    Danikar, you could always save the CGI parameters into a standard CGI file via:

    $query->save(\*FILEHANDLE);

    as documented in the CGI docs.

    For each form submission, you'll need to generate a unique and retrievable key (which is the basis for the associated filename) so that you can identify which file belongs to that particular session. In your form, you'll need to include a hidden variable containing the value of this key so that it can be relayed through the form submission.

    On the receiving side, create a FILEHANDLE to access the contents of your file. You can do this because you know the name of the file (via the hidden variable). There's a little housekeeping that happens afterwards (ie. deletion of the saved/expired CGI files, etc.).

    It looks like this:

    open(IFH, $CGI_filename) || die ... my $query = CGI->new(IFH);
    An advantage to this method is that you can create many bogus form submissions to exercise the various features of your code. Be creative!

    Update: Please be aware that there are some security considerations that go along with this. You need to take the appropriate precautions as you deem necessary.


    Your wish is my commandline.
Re: Forwarding Post Data
by zentara (Archbishop) on Nov 30, 2007 at 14:01 UTC
    Here is a little PostData forwarder I used before. There is some test prints in there, but basically, you just get you concat all your form inputs into a string called $relay, then use LWP to forward $relay onwards.
    #!/usr/bin/perl use warnings; use CGI 'cgi'; use LWP::UserAgent; our ($secure_server_address,$cgi_directory); require './store_cfg'; print "Content-type: text/html\n\n"; #my $test= 'FALSE'; my $test= 'TRUE'; my $relay; my $cgi = new CGI; my %input= $cgi->Vars(); foreach $name (keys %input){ $value = $input{$name}; $relay .= "$name=$value&"; } $relay .= "Ecom_transaction_complete=$test&"; $relay .= "IOC_response_code=0&"; open (RT,">test/respgen.test"); print RT $relay; close RT; my $ua = LWP::UserAgent->new(); my $req = HTTP::Request->new (POST => "$secure_server_address$cgi_dire +ctory/boacc.pl"); $req->content_type('application/x-www-form-urlencoded'); $req->content("$relay"); my $res = $ua->request($req); print $res->as_string;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://654004]
Approved by pKai
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-23 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found