Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Form validation using CGI

by wishartz (Beadle)
on Jul 31, 2008 at 08:54 UTC ( [id://701372]=perlquestion: print w/replies, xml ) Need Help??

wishartz has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Can anybody help me set up validation for my form. I want it to alert the user if fields are left blank.

At the moment when you press the submit button, it goes to the subroutine add_record_two, but I would like it to go through validation first. Here is the code that generates the form.

my %form = Vars(); if ($form{'action'} eq "addrecord") { &add_record; } elsif ($form{'action'} eq "add_record_two") { &add_record_two; } sub header { $title = shift; print qq~ <HTML> <HEAD> <TITLE> $title </TITLE> <basefont face="Trebuchet MS,Arial,Helvetica" size="2"> </HEAD> <BODY bgcolor="#FFFFFF"> <CENTER> <H1>CHGMANT V2</H1> ~; } sub footer { print qq~ <TABLE border=0 cellspacing=3 cellpadding=3> <TR> <TD><a href="chgmant_v4.cgi?action=addrecord"><FONT COLOR="#00 +0000" size=-1>Add A Record</a></TD> <TD><a href="chgmant_v4.cgi?action=edit"><FONT COLOR="#000000" + size=-1>Modify a Record</TD> <TD><a href="chgmant_v4.cgi?action=delete"><FONT COLOR="#00000 +0" size=-1>Delete a Record</TD> <TD><a href="chgmant_v4.cgi?action=search"><FONT COLOR="#00000 +0" size=-1>Search</TD> </TR> </TABLE> </CENTER> </BODY> </HTML> ~; } sub build_record_page { my (%record) = @_; my ($val) = ""; my ($html) = qq~<TABLE border=1 bgcolor="#FFFFFF" cellspacing=0 cellpa +dding=4> <TR bgcolor="#C0C0C0"> <TD colspan=2><CENTER><font size=-1> Record </CENTER>< +/TD> </TR>~; foreach $obj (@db_fields) { if ($obj eq $db_key) { next; # ID's are dynamically made } $html .= qq~<TR bgcolor="#DDDDDD"><TD><FONT SIZE=-1>$d +b_name{$obj}</TD><TD>~; if ($db_type{$obj} eq "text") { # Makes the text box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } $html .= qq~<INPUT TYPE="text" NAME="$obj" siz +e="20" $val>~; } if ($db_type{$obj} eq "drop" && $db_name{$obj} eq "Use +rID:") { # Makes the dropdown box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } print "$record{$obj} $id\n"; # +debug $html .= popup_menu ( -name = +> $obj, -value +s=>\@users); } if ($db_type{$obj} eq "drop" && $db_name{$obj} eq "Typ +e Of Change:") { # Makes the dropdown box if ($record{$obj}) { $val = qq~ VALUE="$record +{$obj}"~; } else { $val = ""; } print "$record{$obj} $id\n"; # +debug $html .= popup_menu ( -name = +> $obj, -value +s=>\@changetype); } elsif ($db_type{$obj} eq "textarea") { # Makes the tex +tarea if ($record{$obj}) { $val = qq~$record{$obj}~; + } else { $val = ""; } $html .= qq~<TEXTAREA NAME="$obj" ROWS="4" COL +S="40">$val</TEXTAREA>~; } $html .= qq~</TD></TR>~; } $html .= "</TABLE><P>"; print $html; } sub add_record { &header("Add a Record"); print qq~ <CENTER> <FORM METHOD=POST> <INPUT TYPE="hidden" NAME="action" VALUE="add_record_two">~; &build_record_page; print qq~ <CENTER> <INPUT TYPE="submit" value="Add Change"></CENTER> <CENTER> </FORM>~; &footer; } sub add_record_two { $form{$db_key} = &get_next_id(); my ($line) = &make_data(%form); open (DATABASE, ">>".$file); if ($flock) { flock(DATABASE, 2) } print DATABASE $line; close (DATABASE); &header("Add a Record Successful - $id"); print qq~<CENTER>Here Is Your Record</CENTER><p>~; my (%result) = get_record($form{$db_key}); print Dumper(%result); if (%result) { &build_record_page(%result); } else { print "Error - No Record Added"; } &footer; }

Thanks

Replies are listed 'Best First'.
Re: Form validation using CGI
by Anonymous Monk on Jul 31, 2008 at 09:23 UTC
Re: Form validation using CGI
by injunjoel (Priest) on Jul 31, 2008 at 20:41 UTC
    Greetings,
    Though it could be fun/good to roll out your own solution, you might be interested in how it has been done (e.g. Data::FormValidator or FormValidator::Simple).
    While we're at it might I suggest looking over HTML::Template and HTML::FillInForm both of which could help you separate out the HTML from your code.
    two weeks in the lab can save you two hours in the library...


    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://701372]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found