By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,692 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,692 IT Pros & Developers. It's quick & easy.

Having a problem processing a form with Perl

numberwhun
Expert Mod 2.5K+
P: 3,503
Hello everyone! I am dabbling in processing forms using Perl and having only just begun, I am already having problems. Go figure. :-)

Top begin with, here it the form's html, its nothing at all complicated:

Expand|Select|Wrap|Line Numbers
  1. <div id="formcontent">
  2.     <h3>The New Information Gathering Form</h3>
  3.     <form action="./cgi-bin/process_form.cgi" method="post">
  4.         <fieldset>
  5.             <legend><strong>Personal Information</strong></legend>
  6.             <label for="firstname">First Name:</label>
  7.             <input type="text" id="firstname" maxlength="25"/><br />
  8.             <label for="lastname">Last Name:</label>
  9.             <input type="text" id="lastname" maxlength="30"/><br />
  10.             <label>Date Of Birth:</label>
  11.             <label for="dobmonth" class="date">Month</label>
  12.             <input type="text" maxlength="2" id="dobmonth" size="2" />
  13.             <label for="dobday" class="date">Day</label>
  14.             <input type="text" maxlength="2" id="dobday" size="2" />
  15.             <label for="dobyear" class="date">Year</label>
  16.             <input type="text" maxlength="4" id="dobyear" size="4" /><br/>
  17.             <label>Gender:</label>
  18.             <input type="radio" name="gender" value="Male" /> <label for="gender" class="sex">Male</label>
  19.                 <input type="radio" name="gender" value="Female" /> <label for="gender" class="sex">Female</label><br />
  20.  
  21.         </fieldset>
  22.         <br />
  23.         <fieldset>
  24.             <legend><strong>Contact Information</strong></legend>
  25.             <label for="address1">Address1:</label>
  26.             <input type="text" id="address1" /><br />
  27.             <label for="address1">Address2:</label>
  28.             <input type="text" id="address2" /><br />
  29.             <label for="city">City:</label>
  30.             <input type="text" id="city" /><br />
  31.             <label for="state">State:</label>
  32.             <input type="text" id="state" /><br />
  33.             <label for="zipcode">Zip Code:</label>
  34.             <input type="text" id="zipcode" /><br />
  35.             <label for="phone">Phone #:</label>
  36.             <input type="text" id="phone" alt="In the format XXX-XXX-XXXX" /><label class="format">(XXX-XXX-XXXX)</label><br />
  37.         </fieldset>
  38.         <br />
  39.         <fieldset>
  40.                <legend><strong>Form Control</strong></legend>
  41.                <input type="submit" value="Submit" class="submit" />
  42.                <input type="reset" value="Reset Form" />
  43.         </fieldset>
  44.     </form>
  45. </div>
  46.  
Please know that with the css I have, it will look a lot prettier. If you want to see the form in all its glory, please see this link

If you put in information and hit submit, you will see that it errors. No, there isn't a 500 page as of yet, but that's not the issue. If I execute the script by hand, I get the following error:

[code]
[Mon Aug 18 10:41:57 2008] process_form.cgi: Use of uninitialized value in read at ./process_form.cgi line 22.
[code]


The following is the script that I have put together to process the above form after submission.


Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. # The following line needed as the host puts a perl
  4. # directory in your site home directory that contains
  5. # ALL the Perl modules that are installed on the site. 
  6. # Its basically everything.
  7. use lib '/home/jkwebdev/perl';
  8.  
  9. ##### use statements #####
  10. use strict;
  11. use warnings;
  12. use CGI::Carp qw(fatalsToBrowser);
  13.  
  14. ##### Variable Declarations #####
  15. my @pairs;
  16. my $pair;
  17. my $value;
  18. my $name;
  19. my %form;
  20. my $buffer;
  21.  
  22. ##### 
  23. if ($ENV{'REQUEST_METHOD'} eq 'POST') {
  24.  
  25.     read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  26.  
  27.     @pairs = split(/&/, $buffer);
  28.  
  29.     foreach $pair (@pairs) {
  30.         ($name, $value) = split(/=/, $pair);
  31.         $value =~ tr/+/ /;
  32.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  33.         $form{$name} = $value;
  34.     }
  35.  
  36. }
  37.  
  38. for my $key ( keys %form ) {
  39.     my $value = $form{$key};
  40.     print "$key => $value\n";
  41. }
  42.  
All I want to do at first is print out all of the values so I can get a visual on it and that isn't even working.

Any help will be greatly appreciated!

Best regards,

Jeff
Aug 18 '08 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
Why aren't you using the CGI module to get the form data Jeff?

Expand|Select|Wrap|Line Numbers
  1. use lib qw(.....);
  2. use strict;
  3. use warnings;
  4. use CGI::Carp qw/fatalsToBrowser/;
  5. use CGI;
  6. my $q =  CGI->new;
  7. my $form = $q->Vars;
  8. print $q->header(),
  9.        $q->start_html();
  10. foreach my $keys (keys %form) {
  11.     print "$keys = $form{$keys}<br/>\n";
  12. print $q->end_html;
  13. exit;
Aug 18 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Is that your website Jeff?
Aug 18 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Why aren't you using the CGI module to get the form data Jeff?
To tell you the truth, I am pretty much just starting to play with CGI and was going off of a tutorial.

I think that I will go and read through the CGI module and use your example. Thanks!

Jeff
Aug 18 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
Is that your website Jeff?
Why yes, yes it is, but don't take it at face value. Its been around a little while and is severely lacking in the update department. That will change though as I am starting to play with things like Drupal and want to revamp the entire thing. (plus maybe even finish it.) :-)

Regards,

Jeff
Aug 18 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
To tell you the truth, I am pretty much just starting to play with CGI and was going off of a tutorial.

I think that I will go and read through the CGI module and use your example. Thanks!

Jeff
You are reading the old tutorial, that site has an updated tutorial using the CGI module. While it is good to know some of the behind the scenes workings of CGI form processing, do not use that code in a live script.
Aug 18 '08 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
You are reading the old tutorial, that site has an updated tutorial using the CGI module. While it is good to know some of the behind the scenes workings of CGI form processing, do not use that code in a live script.
Lovely! You would think that they would take down the old one or at least mark it deprecated or something. Sheesh!! Thanks for the 411, I appreciate it and will check out their other tutorial.

FYI, that link came from a Google. search.

Regards,

Jeff

**update: Ok, I dug out my copy of hooked on phonics and learned that the stuff at the top of the web page in question actually says what you were saying. :-) I think I need sleep!!!
Aug 18 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
Why yes, yes it is, but don't take it at face value. Its been around a little while and is severely lacking in the update department. That will change though as I am starting to play with things like Drupal and want to revamp the entire thing. (plus maybe even finish it.) :-)

Regards,

Jeff
hehehe.... don't worry, we've all been there and done that.
Aug 18 '08 #8

eWish
Expert 100+
P: 971
hehehe.... don't worry, we've all been there and done that.
At least he used the code tags when posting his code :)
Aug 19 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.