Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Automating generation of html form elements

by Anonymous Monk
on Jun 03, 2000 at 02:29 UTC ( [id://16150]=CUFP: print w/replies, xml ) Need Help??

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); }

Replies are listed 'Best First'.
RE: Automating generation of html form elements
by merlyn (Sage) on Jun 06, 2000 at 06:09 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-26 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found