HTML::Defaultify

Pre-fill default values into an existing HTML form
Download

HTML::Defaultify Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • James Marshall
  • Publisher web site:
  • http://www.jmarshall.com/

HTML::Defaultify Tags


HTML::Defaultify Description

Pre-fill default values into an existing HTML form HTML::Defaultify is a Perl module that lets you take an existing HTML page with forms in it, and fill in the form fields according to defaults you give. It's most useful in CGI scripts. One common use is to handle invalid user input-- show them the same form with their previously entered values filled in, and let them correct any errors. Another use might be when letting users edit a database record, such as their account information-- show them an input form with the current values filled in. There are other uses.Other tools can populate form fields, but they require the HTML and program source code to be highly intermingled. In contrast, the approach used here (of populating any existing HTML document) allows a clean separation of the HTML development and the programming. This works much better in projects where the two tasks are done by different people, such as a designer and a programmer.To defaultify a form, use the defaultify() function. The following command is all most people ever need from this module: $new_HTML= &defaultify($HTML, $defaults) ;or, to specify which form to defaultify on a page with multiple forms: $new_HTML= &defaultify($HTML, $defaults, $form_name) ;$HTML is some HTML text, possibly read in from a file; you might even replace the parameter above with "`cat filename.html`". $defaults is a reference to a hash of default values-- the keys of the hash correspond to the names of the form fields, and the values are what to set the defaults to; each hash value may be either a scalar, a reference to a list of scalars (for multiple input fields with the same name), or a list in the form of a "\0"-delimited scalar. $form_name is the name of a form, from the "name" attribute of the < form > tag.The resulting $new_HTML is the same as $HTML, except that all < input > tags, < select >...< /select > blocks, and < textarea >...< /textarea > blocks have been altered to best represent the values given in the defaults hash. This includes clearing fields that have no default given. If $form_name is given, changes are restricted to that form only. Otherwise, all of $HTML is defaultified.The format of the defaults hash is made to accommodate the most common ways that form data sets are represented, including how user input is read by common tools. For example, if you're using the CGI.pm module, you can use the hash reference returned by the Vars() method or function as $defaults (but see below for an easier way if you're using CGI.pm). You can also use the hashes returned from other tools that parse form input, such as the cgi-lib.pl library or the getcgivars() function. In the last two cases, note that those functions return hashes instead of hash references, so you should create a reference when calling defaultify(): %my_defaults= &getcgivars ; $new_HTML= &defaultify($HTML, \%my_defaults) ;Of course, you can always create your own default set: $new_HTML= &defaultify($HTML, { name => 'Coltrane', citycb => } ) ;or the same thing, using a different list format: $new_HTML= &defaultify($HTML, { name => 'Coltrane', citycb => "SF\0LA\0NY" } ) ;As a special case, if you're using the CGI.pm module, you can give a reference to a CGI object as $defaults, and defaultify() will use the current parameters in the object as its default set. For example: $q= new CGI ; $new_HTML= &defaultify($HTML, $q) ;As another special case, if $defaults is undefined, then defaultify() will clear all form fields; in other words, passing undef is the same as passing an empty hash reference.If defaultify() is called in list context, it returns two values: the defaultified HTML as above, and a reference to a hash that contains all the leftover defaults, i.e. those that were not set in any form field. That hash is in the same format as the input defaults hash, and is a subset of it (and any included lists are subsets of their respective lists). It's useful if you need to know which values were set and which were not. Also, passing that hash reference to hidden_vars() and inserting the result into $new_HTML means that all data in the original defaults hash is now represented in the form.Besides defaultify(), this module provides other functions that some users may find helpful. subhash() creates a subset of a hash and returns a hash reference, suitable for use when calling defaultify(); this is useful to set only certain fields in a form. hidden_vars() creates hidden form fields that represent a given set of form data, such as the set of unused defaults returned by defaultify(); hidden_var() creates one well-formatted hidden form field. parse_tag() and build_tag() parse an HTML tag into its tag name and attributes, and rebuild it. For details on these functions, see the EXPORTS section below and the examples in the synopsis above.SYNOPSIS use HTML::Defaultify; # $HTML is a block of HTML with a form, and %my_defaults is a hash of # values to populate the form with. # If $HTML contains multiple forms, you can name which form to populate. $new_HTML= &defaultify($HTML, \%my_defaults) ; $new_HTML= &defaultify($HTML, \%my_defaults, 'form1') ; # If you care which defaults were left over, call it in list context. # Result can be passed to hidden_vars() to generate a set of # < input type=hidden > tags. ($new_HTML, $unused_defaults)= &defaultify($HTML, \%my_defaults) ; $remaining_form_values= &hidden_vars($unused_defaults) ; # subhash() creates a hash that's a subset of a larger hash, similar # to a slice. Useful to partially populate forms. This example # might avoid filling in a "password" field. $new_HTML= &defaultify($HTML, &subhash(\%my_defaults, qw(login type))) ; Requirements: · Perl


HTML::Defaultify Related Software