Google Answers Logo
View Question
 
Q: For validation with perl script ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: For validation with perl script
Category: Computers > Programming
Asked by: stevepr-ga
List Price: $10.00
Posted: 05 Jun 2004 12:12 PDT
Expires: 05 Jul 2004 12:12 PDT
Question ID: 356823
I am trying to get a form validated with a perl script.

I know very litle about perl.  It has to be done in perl because its a
school project.  I am running windows xp with active perl and IIS.

Here is teh Html Form that needs validated.

<html>
<body>

<form name="MyForm" method="post" action="/scripts/11-01.pl">
  <p>Name: 
    <input type="text" name="name" />
  </p>
  <p>Address: 
    <input type="text" name="address" />
  </p>
  <p>Zip code: 
    <input type="text" name="zip" />
  </p>
  <p>Social Security: 
    <input type="text" name="social" />
  </p>
  <p>DOB: 
    <input type="text" name="dob" />
  </p>
  <p>Mothers Maiden Name: 
    <input type="text" name="maiden" />
  </p>
  <p>&nbsp; </p>
  <p>
    <input type="submit" />
  </p>
</form>

</body>
</html>


***END****************************************

I am not shure if this html form is good or if the submit buton needs some help?

Here is the perl scrip so far:

#!c:/perl/bin 
print "Content-type: text/html\n\n"; 
read(STDIN,$tempbuffer,$ENV{?CONTENT LENGTH'});
@pairs=split(/&/,$tempbuffer);

foreach $item(@pairs)
{
 $error = "";
if ($in{'dd'} eq "")
{
  $error += "Please Enter a value.<p>";
}
}



***END****************************************
I will be honest I have been copying and pasting from here and there
for this script but finally called it quits.
Answer  
Subject: Re: For validation with perl script
Answered By: palitoy-ga on 05 Jun 2004 13:12 PDT
Rated:5 out of 5 stars
 
Hello Stevepr

You are very nearly there!

Here is the script that you require:

#====================================================
#!c:/perl/bin 

print "Content-type: text/html\n\n";

$data_length = $ENV{'CONTENT_LENGTH'};
$bytes_read = read(STDIN, $my_data, $data_length);
@name_value_array = split(/&/, $my_data);

foreach $name_value_pair (@name_value_array) {
   ($name, $value) = split(/=/, $name_value_pair);
   $name =~ tr/+/ /;
   $value =~ tr/+/ /;
   $name =~ s/%(..)/pack("C",hex($1))/eg;
   $value =~ s/%(..)/pack("C",hex($1))/eg;

   if (length($value) == 0) {
     print "Please Enter a value.<p>";
   }
   else { print $name . ": " . $value . "<P>"; };
}
#====================================================

You may wish to alter the code slightly depending on whether you wish
to process the name part of the name:value pair passed by the HTML
page.  The code for the HTML page is perfect (as long as the location
of the .pl script is correct).  You may also wish to alter what is
printed out at the end of the script.

If you need any more help please ask for clarification and I will do
my best to help you out.

Request for Answer Clarification by stevepr-ga on 05 Jun 2004 13:24 PDT
Ths is great could I get you to add some coments with in the script
its self.  Kind of explaining whats going on.

Clarification of Answer by palitoy-ga on 05 Jun 2004 13:49 PDT
No problem.  If you have any more questions on this ask away and I
will try to clarify them further.

#====================================================
#!c:/perl/bin 

# ensure the ouput is html friendly
print "Content-type: text/html\n\n";

# as we are POSTing the data from the form we need to 
# use content_length and read()
$data_length = $ENV{'CONTENT_LENGTH'};
$bytes_read = read(STDIN, $my_data, $data_length);

# put the information into an array splitting with &
@name_value_array = split(/&/, $my_data);

# loop through the information
foreach $name_value_pair (@name_value_array) {
   # as each item in the loop is in the format name=value
   # split $name_value_pair into two variables by the = sign
   ($name, $value) = split(/=/, $name_value_pair);
   # clean up the variables replacing the + with a space
   $name =~ tr/+/ /;
   $value =~ tr/+/ /;
   # translate any hex values back into characters
   $name =~ s/%(..)/pack("C",hex($1))/eg;
   $value =~ s/%(..)/pack("C",hex($1))/eg;

   # if the length of the $value variable is zero (ie no data)
   if (length($value) == 0) {
     print "Please Enter a value.<p>";
   }
   # else print the values out
   else { print $name . ": " . $value . "<P>"; };
}
#====================================================

Request for Answer Clarification by stevepr-ga on 15 Jun 2004 19:44 PDT
Thanks for the clarification I dint just want the answer but I also
wanted to understand the language more and learn on what I was doing
wrong.  I just needed an extra hand to be able to finalize this
project.

Clarification of Answer by palitoy-ga on 16 Jun 2004 01:20 PDT
Which parts do you not understand?  Let me know and I will explain them to you.

Request for Answer Clarification by stevepr-ga on 16 Jun 2004 14:49 PDT
No I meant to say thankyou for explaining it!

I got it all.

Steve

Clarification of Answer by palitoy-ga on 17 Jun 2004 01:22 PDT
Sorry, I misunderstood you!  I'm glad I could help.
stevepr-ga rated this answer:5 out of 5 stars

Comments  
Subject: Re: For validation with perl script
From: jesseuml-ga on 15 Jul 2004 05:24 PDT
 
I have a problem related to this discussion that perhaps you can shed
some light on.  As a preamble, please note that I am aware of the Perl
CGI.pm module, but I am trying to demonstrate very basic CGI concepts
and therefore have written the following code without using CGI.pm
capabilities.

Here is a snippet of code that is part of a form processing script
intended to handle both GET and POST methods:

# extract form data regardless of submission method
my $formData ;
print "<p>Request method:&nbsp; <code><b>",
      $ENV{ "REQUEST_METHOD" }, "</b></code></p>" ;
if ( $ENV{ "REQUEST_METHOD" } eq "GET" ) {
  $formData = $ENV{ "QUERY_STRING" } ;
} else {
  read STDIN, $formData, $ENV{ "CONTENT_LENGTH" } ;
}
print "<p>Form data:&nbsp; <code><b>$formData</b></code></p>" ;

This code works perfectly with the Apache server and Perl 5.8.4 on
both Windows 2000 and Linux systems.  With the IIS server on Windows
XP Professional, it works fine for GET methods, but with POST the read
statement simply hangs.  If I comment the read statement out, the
script runs fine, although of course I don't see the data passed from
the form.

I know that POST works with IIS on Windows XP because I can do
something similar using the facilities in CGI.pm.  I simply can't
understand why the read statement is hanging in the above code.  My
code seems to be analogous to similar code that I have found in
numerous books, Web sites, and newsgroup postings.

Do you happen to have any idea or any suggestions of what to try? 
Thanks for any suggestions you can offer.

Jesse

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy