Having a problem processing a form with Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    Having a problem processing a form with Perl

    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:

    Code:
    <div id="formcontent">
    	<h3>The New Information Gathering Form</h3>
    	<form action="./cgi-bin/process_form.cgi" method="post">
    		<fieldset>
    			<legend><strong>Personal Information</strong></legend>
    			<label for="firstname">First Name:</label>
    			<input type="text" id="firstname" maxlength="25"/><br />
    			<label for="lastname">Last Name:</label>
    			<input type="text" id="lastname" maxlength="30"/><br />
    			<label>Date Of Birth:</label>
    			<label for="dobmonth" class="date">Month</label>
    			<input type="text" maxlength="2" id="dobmonth" size="2" />
    			<label for="dobday" class="date">Day</label>
    			<input type="text" maxlength="2" id="dobday" size="2" />
    			<label for="dobyear" class="date">Year</label>
    			<input type="text" maxlength="4" id="dobyear" size="4" /><br/>
    			<label>Gender:</label>
    			<input type="radio" name="gender" value="Male" /> <label for="gender" class="sex">Male</label>
        			<input type="radio" name="gender" value="Female" /> <label for="gender" class="sex">Female</label><br />
    
    		</fieldset>
    		<br />
    		<fieldset>
    			<legend><strong>Contact Information</strong></legend>
    			<label for="address1">Address1:</label>
    			<input type="text" id="address1" /><br />
    			<label for="address1">Address2:</label>
    			<input type="text" id="address2" /><br />
    			<label for="city">City:</label>
    			<input type="text" id="city" /><br />
    			<label for="state">State:</label>
    			<input type="text" id="state" /><br />
    			<label for="zipcode">Zip Code:</label>
    			<input type="text" id="zipcode" /><br />
    			<label for="phone">Phone #:</label>
    			<input type="text" id="phone" alt="In the format XXX-XXX-XXXX" /><label class="format">(XXX-XXX-XXXX)</label><br />
    		</fieldset>
    		<br />
    		<fieldset>
    		       <legend><strong>Form Control</strong></legend>
    		       <input type="submit" value="Submit" class="submit" />
    		       <input type="reset" value="Reset Form" />
    		</fieldset>
    	</form>
    </div>
    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.cg i: Use of uninitialized value in read at ./process_form.cg i line 22.
    [code]


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


    Code:
    #!/usr/local/bin/perl
    
    # The following line needed as the host puts a perl
    # directory in your site home directory that contains
    # ALL the Perl modules that are installed on the site. 
    # Its basically everything.
    use lib '/home/jkwebdev/perl';
    
    ##### use statements #####
    use strict;
    use warnings;
    use CGI::Carp qw(fatalsToBrowser);
    
    ##### Variable Declarations #####
    my @pairs;
    my $pair;
    my $value;
    my $name;
    my %form;
    my $buffer;
    
    ##### 
    if ($ENV{'REQUEST_METHOD'} eq 'POST') {
    
    	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    	
    	@pairs = split(/&/, $buffer);
    	
    	foreach $pair (@pairs) {
    		($name, $value) = split(/=/, $pair);
    		$value =~ tr/+/ /;
    		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    		$form{$name} = $value;
    	}
    
    }
    
    for my $key ( keys %form ) {
        my $value = $form{$key};
        print "$key => $value\n";
    }
    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
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Why aren't you using the CGI module to get the form data Jeff?

    Code:
    use lib qw(.....);
    use strict;
    use warnings;
    use CGI::Carp qw/fatalsToBrowser/;
    use CGI;
    my $q =  CGI->new;
    my $form = $q->Vars;
    print $q->header(),
           $q->start_html();
    foreach my $keys (keys %form) {
        print "$keys = $form{$keys}<br/>\n";
    print $q->end_html;
    exit;

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Is that your website Jeff?

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by KevinADC
        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

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by KevinADC
          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

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by numberwhun
            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.

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by KevinADC
              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!!!

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by numberwhun
                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.

                Comment

                • eWish
                  Recognized Expert Contributor
                  • Jul 2007
                  • 973

                  #9
                  Originally posted by KevinADC
                  hehehe.... don't worry, we've all been there and done that.
                  At least he used the code tags when posting his code :)

                  Comment

                  Working...