Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

CGI Post query

by ant (Scribe)
on Jul 05, 2006 at 08:05 UTC ( #559264=perlquestion: print w/replies, xml ) Need Help??

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

Its been a while.
Here's a snippet of my code
print header(); if ($ENV{'REQUEST_METHOD'} eq 'POST') { read (STDIN, $input, $ENV{'CONTENT_LENGTH'}) || print "could not e +xecute query <br>"; print "got into POST - $input $ENV{'CONTENT_LENGTH'} <br>" . <STDI +N>; @pairs = split(/&/, $input); }
The output is 'could not execute query '
howvever, when I replace print header with  print "Content-type: text/html","\r\n\r\n"; it all works fine. It seems with print header the STDIN cannot read the HTML form. Below this code, the HTML is formed with the form tag and method = 'post'.
Any ideas why this way worked would be great.

Replies are listed 'Best First'.
Re: CGI Post query
by gellyfish (Monsignor) on Jul 05, 2006 at 09:28 UTC

    It is not recommended to mix subroutines from CGI and hand-rolled parameter parsing: you will find that CGI has already read and exhausted STDIN after calling header(). You almost certainly want to use CGI's param() method to access the CGI parameters, rather than rolling your own.


      Thanks, for the advice, I took a look at the CGI documentation that you had linked and it covers putting forms into a hash with this piece of code
      use CGI ':cgi-lib'; my $params = Vars; print $params->{user};
      I used the other method a few years ago and although I never encountered any problems with it, this way I think is much better with the same results.
      It's good to learn something new everyday!
        You should unlearn this one though... it will come to bite you when you are using checkboxes or select-multiple option boxes. Understand that there can be multiple values for a given param, and simply ask for them as you need them. I really don't see what's so hard about:
        my $last_name = param('last_name'); # only one last name ... my @colleges_attended = param('colleges_attended'); # possibly 0 or ma +ny colleges attended
        That is, asking for it as you need it. Why does everyone wanna keep copying it into a hash? (It boggles me.)

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        I would second the advice above about not rolling your own. Ovid has a nice explanation of ways rolling your own can bite you in his online cgi course here
Re: CGI Post query
by gellyfish (Monsignor) on Jul 05, 2006 at 08:38 UTC

    You haven't imported the header subroutine. You should either do use CGI qw(header); ( or use CGI qw(:standard) if you intend to use other functions from the module.) Or you can specify the full package name of the subroutine - i.e. print CGI::header();


      I was just trying to keep the code to a minimum however just to add a little more code, at the top of the program was
      use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser);
Re: CGI Post query
by wazoox (Prior) on Jul 05, 2006 at 08:18 UTC
    print header; looks weird, header looks like a bareword to me. Better do  print header(); Then you'll probably find that you forgot to use CGI or something similar :)
      Quick Update
      I tried the print header() and that still did not work. I very definetly had use CGI at the top of the script.
      I inherited the code in a new job, and the previous programmer who was a DBA and not a Perl programmer and for each HTML field had
      my $something = param( 'something' );
      over and over again, I was just changing the program so that all the values were loaded into a hash.

        Just having use CGI; isn't enough. If you're using the function interface to then you need to use one of the import tags - use CGI ':standard'; or use CGI ':cgi'; or something like that.

        And if you're using, then you should really use the param method to access the parameters (as it seems the previous programmer did). Writing your own parameter parser as you seem to be trying to do is pointless and you're almost certain to miss out one of the corner cases that handles.


        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2023-11-28 13:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found