Use a central resource hash to store html form element attributes/description information by name. Then use CGI.pm to generate these elements dynamically.

This gets more interesting with passing in default values and options, like 'nolabels', for dropboxes, checkbox groups, multiselect, radiobuttons, i.e. when you have to pass pointers to arrays or hashes, or flags. See the 'sendemail' field example in the code.

Also, using CGI.pm helps you automate the process of validating the data passed in because the CGI object rebuilds its state with the html form variables passed in. Ask if you don't know what I mean by this.

Note: you might want to make two hashes so extra, unused attributes don't appear in your html form tags. But I didn't worry about it for now.

Send questions to pcleddy@yahoo.com. Sorry if this is not crystal clear.

# $invitestable is a pointer to a array of hashes. # This 'table' contains prepopulated rows of information. # Keys in these hashes are the same as the elements in # @fieldorder below. Uh, oh yea, there is a key called # 'id' as well, very important. $q = new CGI; <fill up $invitestable somehow with code here> $fielddefs = &defineformfields; @fieldorder = ('sendemail','firstname', 'lastname', 'companyname', 'em +ail', 'projectname', 'clientid'); my (@rows); foreach my $row (@$invitestable) { foreach my $element (@fieldorder) { my $function = $fielddefs->{$element}->{function}; $fielddefs->{$element}->{name} = $element . '.' . $row->{id}; $row->{html}->{$element} = $q->$function($fielddefs->{$element +}); } } sub defineformfields { my %fielddefs = ( sendemail=>{ order=>1, function=>'checkbox_group', values=>['o +n'], nolabels=>1, defaults=>['on'], title=>'Send' }, email=>{ order=>3, function=>'textfield', size=>11, title=>'Em +ail' }, date=>{ order=>2, function=>'textfield', size=>12, title=>'Dat +e' }, fullname=>{ order=>2, function=>'textfield', size=>8, title=>' +Name' }, firstname=>{ order=>2, function=>'textfield', size=>8, title=> +'First Name' }, lastname=>{ order=>2, function=>'textfield', size=>9, title=>' +Last Name' }, companyname=>{ order=>4, function=>'textfield', size=>12, titl +e=>'Company Name' }, projectname=>{ order=>4, function=>'textfield', size=>10, titl +e=>'Project Name' }, clientid=>{ function=>'hidden' } ); return (\%fielddefs); }

In reply to Automating generation of html form elements by Anonymous Monk

Title:
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, details, 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, summary, 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.