in reply to Converting a PHP Registration Form to Perl

I might be missing just how complicated this form is, but I've never created a form to hard to be handled with HTML::Template and HTML::FillInForm

I've included a sample of how we use CSS for our structure, and Perl for form validation, untainting, and how we return a form if errors. And if you're interested, I've created a formgenerator that writes all the HTML, SQL, and Perl for each field and type--msg me.

HTML: <tmpl_if errorlist> <p>Thanks for your inquiry, but there were error(s):</p> <tmpl_loop name="errorlist"> <p class="error"><tmpl_var name="errormsg"></p> </tmpl_loop> </tmpl_if> <div class="frow"> <span class="blabel"></span> <span class="frm"><i>(Bold fields are mandatory)</i></span> </div> <form action="cgi-bin/contact.pl" method="post"> <div class="frow"> <span class="label">Send To:</span> <span class="frm"> <select name="sendto"> <option value="1">General</option> <option value="2">Brad</option> <option value="3">Kathy</option> <option value="4">Lauren</option> </select> </span> </div> <div class="frow"> <span class="blabel">First Name:</span> <span class="frm"><input type="text" name="first" size="35" maxleng +th="20" value="<tmpl_var name='first'>" /> </span> </div> <div class="frow"> <span class="label">Last Name:</span> <span class="frm"><input type="text" name="last" size="35" maxlengt +h="30" value="<tmpl_var name='last'>" /> </span> </div> <div class="frow"> <span class="label">Company:</span> <span class="frm"><input type="text" name="company" size="35" maxle +ngth="30" value="<tmpl_var name='company'>" /> </span> </div> <div class="frow"> <span class="label">Announcements:</span> <span class="frm"><input type="checkbox" name="notify_ok" value="1" + /> </span> </div> CSS: div.frow { clear: both; padding-top: 9px; } div.frow span.label { float: left; width: 80px; text-align: right; font-family: arial, helvetica, sans-serif; font-size: 11px; padding-top: 3px; } div.frow span.blabel { float: left; width: 80px; text-align: right; font-family: arial, helvetica, sans-serif; font-size: 11px; font-weight: bold; padding-top: 3px; } div.frow span.frm { float: right; width: 500px; text-align: left; font-family: arial, helvetica, sans-serif; font-size: 11px; } PERL: #first, validates input with validate module my ($error, @errors, @errorlist); my $sendto = $query->param('sendto'); my $fifvalues = { sendto => $sendto }; #use HTML::FillInForm for selec +ts and checkboxes (my $first, $error)= Validate->limitedtext ($query->param('first'),20, +1); if ( $error-> { msg } ) { push @errors, "<b>First name</b> $error-> +{ msg }" } (my $last, $error) = Validate->limitedtext ($query->param('last'),30 +,0); if ( $error-> { msg } ) { push @errors, "<b>Last name</b> $error->{ + msg }" } (my $company, $error) = Validate->limitedtext ($query->param('compa +ny'),60,0); if ( $error-> { msg } ) { push @errors, "<b>Company</b> $error->{ m +sg }" } my $notify_ok = $query->param('notify_ok'); $fifvalues = { notify_ok => $notify_ok }; #handle errors and return form if necessary if (@errors) { foreach (@errors) { my %one_record = ( errormsg => $_, ); push (@errorlist, \%one_record); } $template = HTML::Template->new( filename => '/usr/www/users/somename/contact.tmpl', associate => $query, die_on_bad_params => 1); $template->param(errorlist => \@errorlist); print "Content-type: text/html\n\n"; my $html = $template->output(); if ($fifvalues) { my $form = new HTML::FillInForm; my $page = $form->fill(scalarref => \$html, fdat => $fifvalues); print $page; } else { print $html; } exit(); } VALIDATE: sub limitedtext { my ($class, $value, $len, $mand) = @_; if (!$value && $mand) { return (undef, { msg => 'cannot be blank' }); } elsif ($len && (length($value) > $len) ) { return (undef, { msg => 'is limited to '.$len.' characters.' }); } elsif ($value && $value !~ /^([.,-]*\w[\w .,-]*)$/) { return (undef, { msg => 'can only use letters, numbers, spaces a +nd - . ,' }); } else { return "$1"; } }

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re^2: Converting a PHP Registration Form to Perl
by ghenry (Vicar) on Sep 08, 2005 at 20:33 UTC

    Excellent, but I think we have made our form complicated. There's only one sql call, and the rest is in a trigger.

    If you both want to see the code, I can paste all the different elements in.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!