in reply to cgi problem

I saved your code on my server as foo.cgi and accessed it with the GET parameter company=foo. Every thing worked fine ... after i changed the content header to display HTML instead of text. Hmmmm, how about a replacement?
#!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); print header, start_html('Test CGI'), start_form, 'Company Name ', textfield('company'), br, 'Last Name ', textfield('last'), br, 'First Name ', textfield('first'), br, 'Serial Number ', textfield('serial'), br, 'Meter Reading ', textfield('meter'), br, submit('Submit'), end_form, ; if (param('Submit')) { my $company = param('company'); my $last = param('last'); my $first = param('first'); my $serial = param('serial'); my $meter = param('meter'); print hr, h3('You Entered'), pre(" COMPANY: $company LAST: $last FIRST: $first SERIAL: $serial METER: $meter "), ; } print end_html;
Instead of typing all that HTML markup yourself, why not just let CGI.pm do it for you? :) I do applaud you for using Taint mode, by the way ... also, take a look at HTML::Template to keep your HTML out of your Perl code.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: cgi problem
by Anonymous Monk on Dec 23, 2002 at 01:21 UTC
    thanks jeffa,
    but do you know why I still am unable to capture the variables from my html page. For the POST method it does in fact get to the cgi script and perform my little "INFO" routine but the values for the variables are empty.

    Thanks again

      Sorry, but i really just don't see what it could be with the info i have in front of me.

      <update>
      but gjb has eyes of gold ... gjb++
      </update>

      Off hand however, your INFO subroutine is using global variables, which is generally Not a Good Thing ™. Try passing it values instead:
      sub INFO { my ($company,$last,$first,$serial,$meter) = @_; print "COMPANY: $company\n"; print "LAST: $last\n"; print "FIRST: $first\n"; print "SERIAL: $serial\n"; print "METER: $meter\n"; } # you can call it like so: &INFO($company,$last,$first,$serial,$meter);
      Since all the variables have the same names, it seems kind of silly to pass them explicitly, but this is good practice to get into in the long run. Personally, i'd find a way to utilize a hash (and subroutines that print are not very portable, so return a string instead):
      use strict; use warnings; my %hash = ( COMPANY => 'Acme', LAST => 'Scott', FIRST => 'Randolph', SERIAL => 'number', METER => 'maid', ); print &INFO(%hash); sub INFO { my %params = @_; my $output; $output .= "$_ = $params{$_}\n" for keys %params; return $output; }
      Tie::IxHash will keep your keys ordered, should you need that.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)