If you have a pre-generated form that you read from disk and want to make it "sticky", you should look at using HTML::FillInForm. Simple use would be:
my $q = CGI->new;
my $fif = HTML::FillInForm->new;
my $output = $fif->fill(file => 'htmlfile.html', fobject => $q);
print $output;
Note that this code will fill the form with ALL the parameters passed from $q, but you can change and delete the parameters as needed prior to calling fill so that you can remove bad input.
A second option or for use in addition would be to use a templating system such as HTML::Template or Template Toolkit.
Hope this helps.
Sean | [reply] [Watch: Dir/Any] [d/l] |
The self_url method of a CGI object will return a URL to the current page that preserves the value of the form elements. You can invoke the delete method first to remove those parameters that you don't want reflected in this URL. | [reply] [Watch: Dir/Any] |
Not sure of your set-up but from what I read it sounds like you should consider using a scripting language to do the form processing locally.
For example, javascript is widely used in this siutation.
By doing the processing *locally* it saves the time otherwise required to
- send duff data to the server
- process it remotely
- return results
- re-write page
Using the "onSubmit()" attribute associated with <FORM>, and with a local scripting language it's a doddle to do the data validation, alert the user to the error(s) and avoid submitting the form. For more info on javascript search webreference.com.
| [reply] [Watch: Dir/Any] |
You still need to do the form validation at the server. If the user has disabled javascript then your solution will not work. Also, unless you have written your form processing correctly there is nothing to stop a user saving your form to their local computer and editing it - even re-writing your javascript if they need to.
However, well written javascript will enable a user to get immediate feedback on the form data without submitting it. Just don't rely on it.
| [reply] [Watch: Dir/Any] |
Using the aforementioned HTML::Template is a good solution to repopulating an HTML form after Perl-side validation. I do this all day long. H::T has a built-in function that preserves the values from the form:
...
my $query = new CGI;
...
my $template = HTML::Template->new( filename => "foobar.tmpl",
associate => $query);
However, one problem in any of the methods described above, is resetting radio buttons, check boxes, or select/option dropdowns. Besides using the associate function in H::T, you need to set your returning params to trigger which buttons, boxes, or dropdowns have been selected. Here's my messy solution (I'm open to new ways, monks):
Perl:
my $vote = $query->param('vote');
my ($voteyes, $voteno);
if ($vote == 1) {
$voteyes == 1;
} elsif ($vote == 2) {
$voteno == 1;
}
$template = param(voteyes => $voteyes,
voteno => $voteno);
HTML:
Yes: <input type="radio" name="vote" value="1"<tmpl_if voteyes> checke
+d</tmpl_if> />
No: <input type="radio" name="vote" value="2"<tmpl_if voteno> checked<
+/tmpl_if> />
Just a friendly "heads-up." Good luck.
—Brad "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
| [reply] [Watch: Dir/Any] [d/l] [select] |
Thank you everyone for your very useful comments and suggestions. Due to some of the modules that you suggested using not being installed (and me not knowing what to do about getting them installed - I've only worked here a week and a half and never used Perl before, but I digress) I decided that the easiest thing to do would probably be to code the form into the Perl and use self_url. It's working perfectly (well, that bit of the script :P ) and it certainly helped me get my head round some of the things I had been trying to avoid in Perl (read: OOP).
Thanks again :)
| [reply] [Watch: Dir/Any] |
| [reply] [Watch: Dir/Any] |