form validation with perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting form validation with perl
# 1  
Old 12-20-2007
form validation with perl

Hey guys, I'm just messing around with a perl webpage. The idea is to make a simple validation form that will later insert a record into my DVD database. it's all very basic at the moment, and I worked up my script from the form validation example I found on this website: http://www.elated.com/articles/form-validation-wi
th-perl-and-cgi/ . When I put this script on my webserver, it works fine.

My form just has 3 text fields and a button. The problem is, when I enter text and try to submit, the text I entered dissapears and I get the error messages, stating that the boxes are empty. For the life of me, I can't figure out what is wrong with it. Mind you, I've just started learning PERL in bits and pieces, from examples I find on the web, so if I'm missing the obvious here, please don't shoot me .

Any help or pointers is more than welcome

Anyway, here's the script:
Code:
#!/usr/bin/perl
use CGI;

# Create the CGI object
my $query = new CGI;

# Output the HTTP header
print $query->header();

# Process form if submitted; otherwise display it
if ( $query->param("submit") )
{
process_form();
}
else
{
display_form();
}

sub process_form
{
if ( validate_form() )
{
print <<END_HTML;
<html><head><title>Thank You</title></head>
<body>
Thank you - your form was submitted correctly!
</body></html>
END_HTML
}
}

sub validate_form
{
my $dvd_title = $query->param("dvd_title");
my $number_of_discs= $query->param("number_of_discs");
my $year = $query->param("year");
my $error_message = "";

$error_message .= "Please enter the DVD title<br>" if ( !$dvd_title );
$error_message .= "Please enter the number of discs<br>" if ( !$number_of_discs );
$error_message .= "Please enter the year<br>" if ( !$year );

if ( $error_message )
{
# Errors with the form - redisplay it and return failure
display_form ( $error_message, $dvd_title, $number_of_discs, $year );
return 0;
}
else
{
# Form OK - return success
return 1;
}
}

sub display_form
{
my $error_message = shift;
my $dvd_title = shift;
my $number_of_discs = shift;
my $year = shift;

# Remove any potentially malicious HTML tags
$dvd_title =~ s/<([^>]|\n)*>//g;
$number_of_discs =~ s/<([^>]|\n)*>//g;
$year =~ s/<([^>]|\n)*>//g;

print <<END_HTML;
<html>
<head><title>Add a DVD</title></head>
<body>

<form action="adddvd3.pl" method="post">
<input type="hidden" name="submit" value="submit">

<table>
<tr><td colspan="2" style="text-align:center">$error_message</t
d></tr>
<tr><td>DVD Title:</td><td><input type="text" name="dvd_title" value="$dvd_title"></td></tr>
<tr><td>Number of Discs:</td><td><input type="text" name="number_of_discs" value="$number_of_discs"></td></tr>
; <tr><td>Year:</td><td><
;input type="text" name="year" value="$year"></td></tr>
<tr><td colspan="2" style="text-align:center"><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>

</body></html>
END_HTML
}

# 2  
Old 12-20-2007
Quote:
Originally Posted by LNC
Code:
#!/usr/bin/perl
use CGI;

# Create the CGI object
my $query = new CGI;
...
sub validate_form
{
my $dvd_title = $query->param("dvd_title");
my $number_of_discs= $query->param("number_of_discs");
my $year = $query->param("year");
my $error_message = "";
...

Take with a grain of salt, I don't nomrally use perl in an OO fasion like you are here.
I think you might be getting caught up by the scoping of your CGI object.
I would suggest either passing it to the validate_form function or dropping the 'my' keyword when you create it.
# 3  
Old 12-20-2007
I put this in my Web server, entered something and submit, and got

"Thank you - your form was submitted correctly!"

Is this your expected result? The posted script seems to look fine to me.

The debate between the procedural and the OO camp has always been there.

Personally I see nothing wrong with using OO-style in Perl. One major Perl-based discussion forum (I don't give the name here as it is semi-commercial) is fully OO, and IMO OO in Perl is nothing more than subroutines that happen to contain "instance" data with "packages" offering namespace separation. Neither of them I would agree being cited as hindrance rather than help. In fact, in recent years PHP is starting to aware they need this kind of things now when they attract more sophisticated developers who have experiences with other language environments.

Scoping in Perl can be complex, but once you understand it, it is a useful device to prevent variable collision and inadvertently stomping on global variables you don't intend to access. If you have happened to write any Perl thing that spans more than 50 script files or so, you will know this is far easier to happen than you would imagine.

By the way, it is a consensus in the Perl community that new code should be runnable with "use strict", and variables without explicitly marked 'my', 'our', or 'local' (with package qualification), will not pass the strict test.

Surely one is free to select his/her approach. My opinion is we should be open about others' choices even if we have a different stance personally.
# 4  
Old 12-20-2007
Quote:
Originally Posted by cbkihong
The debate between the procedural and the OO camp has always been there.

Surely one is free to select his/her approach. My opinion is we should be open about others' choices even if we have a different stance personally.
Definately no issue with OO, I'm just pointing out that my advise might be flawed as I'm not very familiar with OO Smilie
# 5  
Old 12-21-2007
That was indeed what I was aiming for, however, on my webserver it doesn't seem to work, whereas the example script in the link I posted does. If needed, I could pm a link to the script on my server. I don't really want to post it here, as it is just a private server in my house.
I can't figure out why one script would work fine, and the other wouldn't.
# 6  
Old 12-21-2007
It is difficult to provide concrete help:

1) You previously mentioned the posted script didn't work, but my test of the script verbatim you said was what you intended to get;

2) You did not tell what are the differences between the script you deployed and the one you posted. Of course, you should post the simplest variation of your script, in full, that is sufficient to demonstrate and have others reproduce the problem.

I have not visited the link you provided, so I do not quite understand how I can help you with this. If you have further details to share, please feel free to post again.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl: How to improve with minimal validation of its input??

The Code: #!/usr/bin/perl use strict; use warnings; print "Please enter numbers, separated by commas: "; my $data=<STDIN>; chomp $data; my @dataset=split(/,/, $data); my $sum = 0; foreach my $num (@dataset) { $sum += $num; } my $total_nums = scalar(@dataset); my $mean =... (1 Reply)
Discussion started by: 300zxmuro
1 Replies

2. Programming

help need in the perl script that create one xml file form multiple files.

Hi every one, Please excuse me if any grammatical mistakes is there. I have multiple xml files in one directory, I need to create multiple XML files into one XML file.example files like this</p> file1:bvr.xml ... (0 Replies)
Discussion started by: veerubiji
0 Replies

3. Shell Programming and Scripting

Form validation in PHP

My form validation script looks like this of the form like this.... <html> <form method="POST" action=""> <table > <tr> <td >Name:</td> <td ><input type="text" name="name" size="30%"></td> </tr> <tr> <td >Phone:</td> <td ><input type="text" name="phone" size="30%"></td> </tr>... (0 Replies)
Discussion started by: gameboy87
0 Replies

4. Shell Programming and Scripting

How to make a long print string to shotcut form in perl?

print "1.readfromfile\n2.add_ex1(4,5)\n3.add_ex2(11,5)\n4.add_ex3(9,3)\n5.add_ex4(91,4)\n"; How to do it in this form: print "1.readfromfile\n 2.add_ex1(4,5)\n 3.add_ex2(11,5)\n 4.add_ex3(9,3)\n 5.add_ex4(91,4)\n"; (3 Replies)
Discussion started by: cola
3 Replies

5. Shell Programming and Scripting

Perl script :- Phone number validation

Hi All, I am doing a perl script validation for Phone numbers. The normal phone number format is 01-32145. I need to do two validations for the phone number 1) A valid phone number can have at least two digits as prefix and at least five digits as postfix. e.g. 01-01011 2) A... (5 Replies)
Discussion started by: subin_bala
5 Replies

6. Shell Programming and Scripting

on HTML form, Call Expect in Perl problem

Hi I have a successfullly run perl script (by issuing command "perl sub.pl" under shell mode) and this sub.pl will call sub.exp successfully. The sub.exp expect script is basically to login to a server and run some commands and put the output into a sub.txt file, it takes about 5 seconds to... (0 Replies)
Discussion started by: cxbest
0 Replies

7. Shell Programming and Scripting

perl linux file name validation

Hi Everyone, #!/usr/bin/perl $a = ".a!"; if ($a =~ s///g) { print "invalid file name\n"; } else { print "valid file name\n"; } but the output is: Invalid range "_-." in regex; marked by <-- HERE in m// at ./a.pl line 5. the linux file name should be A-Z, a-z,... (8 Replies)
Discussion started by: jimmy_y
8 Replies

8. Web Development

in cgi perl script a form

hi,i hav a form in cgi perl script.this script accepts a value from user from another html form, and depending upon this value,i need to disable /enable radio buttons in cgi-perl script wen second page is displayed on executing cgi perl script.how do i do it using javascript? (0 Replies)
Discussion started by: raksha.s
0 Replies

9. Shell Programming and Scripting

perl cgi form action target

Hello All, I was trying to come up with a form using perl cgi. I then created a frame to show the output of the form. Refer below print $display_form->start_form(-title=>"Updateuser", -style => 'font-size: 9pt; color: #202020 ; font-family: Verdana', action=>"${DOCROOT}updateUser.pl",... (4 Replies)
Discussion started by: garric
4 Replies
Login or Register to Ask a Question