Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Practical application of HTML::FillInForm, used in conjunction with my favorite templating module, HTML::Template, is not well documented anywhere that I could find. Here's my take.

My biggest use of HTML::FillInForm will be for re-populating, selecting, or checking all the input types in my forms when returning the form for the user to correct a validation error, or when calling up a DB record to be edited. The associate function in H::T only "remembers" text-type input, so to avoid lots of convoluted javascript and Perl, I found the ideal tool in HTML::FillInForm. I did consider using CGI's very own popup_menu feature, but I am trying to keep my HTML completely separate from my Perl.

Anyway, here are some methods I've discovered in experimenting with the two modules:

PERL: my $q = new CGI; my $fvalues = $q -> Vars; #get all the value pairs from the submitted + form my $name = "Mr.". $q->param('name'); #alter one of them $fvalues->{name} = $name; my $template = HTML::Template->new( filename => "../form.tmpl"); my $html = $template->output; my $form = new HTML::FillInForm; my $page = $form->fill(scalarref => \$html, fdat => $fvalues); print "Content-type: text/html\n\n"; print $page; HTML: <form action="./" method="post"> <input type="text" name="name" size="30" value="<tmpl_var name>" /> +<br /> <input type="text" name="address" size="30" /><br /> <select name="choices"> <option value="">Select...</option> <option value="1">Ciara</option> <option value="2">Lauren</option> <option value="3">Brian</option> </select><br /> <input type="submit"> </form>

Note: a value passed in the hash ref to fdat trumps assigning a value to a <tmpl_var> of the same name . So, this will not work:

my $fvalues = $q -> Vars; my $name = "Mr.". $q->param('name'); #alter one of them my $template = HTML::Template->new( filename => "../form.tmpl"); $template->param( name = $name ); my $html = $template->output;

However, this will:

my $fvalues = { address => '123 Main Street', choice => '2' }; #BTW, if the form tag is select with multiple attribute, use a referen +ce to an array of the values: #my @choices = (1, 3); #$fifvalues->{'choices'} = \@choices; my $name = "Mr.". $q->param('name'); #alter one of them my $template = HTML::Template->new( filename => "../form.tmpl"); $template->param( name = $name ); my $html = $template->output;

The above example can be used to populate a new form, e.g., when pulling up a DB record for editing.

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

In reply to Re: HTML::FillInForm by bradcathey
in thread HTML::FillInForm by MrCromeDome

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    Discipulus I'd rather say good weak start..

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (8)
    As of 2018-05-28 08:24 GMT
    Find Nodes?
      Voting Booth?