Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POST method

Status
Not open for further replies.

TyzA

Programmer
Jan 7, 2002
86
BE
Hi,

I'm new to CGI and i have a problem with the post method.
In my HTML I have a small form that uses the POST method.
The problem I have, is that in my CGI (Perl) script the string i receive from STDIN is empty. I've tried different way to do it but it never works.
I use: read (STDIN, $input, $ENV{CONTENT_LENGTH}).
I'm able to print the content length, but $input is always empty. It prints nothing

Hope you guys can help me.

Thanks in advance,

Tijs Programming is like sex: one mistake and you have to support it for the rest of your life.
 
You could try using the param command. If the name of your fields in your form were say, 'Name' and 'Age', you could use:
$name = param('Name');
$how_old = param('Age');

Hope this helps.

I don't make mistakes, I'm merely beta-testing life.
 
Given this cgidecode sub routine (printed in nearly every book recently written about cgi with perl), you can build a hash containing the named HTML inputs and their associated values.


Code:
#!/usr/local/bin/perl
%FORM = &cgidecode;

# if the html form contained a 'name' input,
$name = $FORM{'name'};

sub cgidecode 
    {
    local(%vars, $val, $key, $last, $buffer, $pair, @pairs);

    # Checking the form method (GET or POST) used in the 
    # HTML code. POST method sends data to standard input, 
    # but GET adds it to the URL and stores it in 
    # QUERY_STRING.
    if ($ENV{'REQUEST_METHOD'} eq "POST") 
        {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        }
    else 
        {
        $buffer = $ENV{'QUERY_STRING'};
        }

    # Splitting up the data fields and store 
    # in array @pairs, they are seperated with &
    @pairs = split(/&/, $buffer);

    # Splitting the variable names and the values 
    # and storing them in the assoc. array %vars
    foreach $pair (@pairs) 
        {
        ($key, $val) = split(/=/, $pair);
        $val =~ s/\+/ /g;
        $val =~ s/%(..)/pack("C",hex($1))/eg;
        if ($key eq $last) {$vars{$key} .= " ".$val; }
        else { $vars{$key} = $val; }
        $last = $key;
        }
    return(%vars);
    }

If you understand what that is doing, then I encourage you to use Johnny's approach. The CGI.pm module eliminates a lot of labor in many instances. (Yes, I know it has some over head, but, in general CPU cycles are much cheaper than brain cycles and it can be artfully use'd'.)

If you use the CGI.pm module, the code above looks like,
Code:
#!/usr/local/bin/perl -T
use strict;
use CGI;
my $cgi_object = new CGI;
my $name = $cgi_object->param('name');

There are a few faqs in this forum (see the tabs at the top of the frame). You might benefit from reading through the one on the basics of CGI.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top