Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Remembering values from an HTML form

by Nevtlathiel (Friar)
on Jan 14, 2005 at 12:06 UTC ( #422208=perlquestion: print w/ replies, xml ) Need Help??
Nevtlathiel has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to write a program that will process a form and display it again if there were values entered that weren't valid, while retaining the rest of the values. I have the form written in HTML already and at the moment to show it again am getting my script to search through the HTML document and print everything inside the <form>...</form> tags again. I can see that I'd be able to do this if I coded the form within the perl script but is it possilbe to do it while reading the form in from a file?

Thanks for your help :)

Comment on Remembering values from an HTML form
Re: Remembering values from an HTML form
by aquarium (Curate) on Jan 14, 2005 at 12:40 UTC
    if the html form can be "put into action" as a url, then you just append the already filled in values in the url
    the hardest line to type correctly is: stty erase ^H
Re: Remembering values from an HTML form
by macPerl (Beadle) on Jan 14, 2005 at 12:43 UTC

    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.

      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.
Re: Remembering values from an HTML form
by srdst13 (Pilgrim) on Jan 14, 2005 at 12:45 UTC

    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
Re: Remembering values from an HTML form
by eieio (Pilgrim) on Jan 14, 2005 at 13:17 UTC
    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.
Re: Remembering values from an HTML form
by bradcathey (Prior) on Jan 14, 2005 at 16:35 UTC

    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
      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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://422208]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-08-23 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls