Re: Review Web Form Entry Before Final Submission
Sean Quinlan <[email protected]>
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Message-ID | <[email protected]> |
On Thu, 2005-03-17 at 14:11 -0500, Douglas S. Davis wrote:
> Hi,
>
> I write a lot of programs in Perl that display a web form, and the user then
> fills it in, and I like to give them a chance to review the information that
> they've submitted and have the opportunity to go back (via the Back button
> in their browser) to make changes before the final "set in stone"
> submission.
>
> What's the best way to do this?
I've usually used a cookie. If the size of the information or security
considerations are such that that's not feasible, then I return an md5
hex key as the cookie and store the input in the DB with that as the
key. I generally just use %params = $cgi->Vars; to get the form data and
stringify %params with Data::Dumper. Some field types can require
special handling and of course YMMV.
I also have a little toolbox of functions for generating dropdowns,
radio buttons and such so I don't have to rewrite adding "select" to the
appropriate options in the HTML over and over.
> What I've been doing is this:
>
> A Perl script creates the form (or sometimes it's just a standard HTML
> page).
>
> The user fills it in then clicks "Preview" or some such button.
>
> The form data is processed by a ReadParse subroutine from cgi-lib.pl and
> then spit back out as a webpage to show the data the user submitted.
>
> The data is also in a form on that page with all of the data as hidden
> fields which if they click Submit will then be sent to a MySQL database.
>
> If they click the Back button in their browser instead of Submit, they can
> make changes, Preview and Submit/Edit again ad infinitum.
>
>
>
> It's rather tedious to have to spit their data back at them and have it all
> in a form as hidden fields, but I haven't figured out a better way to do it.
>
> I'd like to have the data go to a MySQL database when they click Preview,
> and then if they click Back and edit the info, the data in the database
> would just be updated, but how would I do that? They'd have to get some
> type of unique ID that would be present in the page when they go back to it
> or it would have to be present when they first load the web form so that the
> data could be matched up.
>
> I'm sure there are much better ways to do it than what I'm doing. Please
> enlighten me.
As mentioned above, variations based on the concept below, storing the
key as a cookie or hidden field as appropriate.
Dumper to turn a data structure (all the form values) into a string that
can be later eval'd back into a data structure:
http://search.cpan.org/~ilyam/Data-Dumper-2.121/
md5_hex to generate a unique key based on the data.
http://search.cpan.org/~gaas/Digest-MD5-2.33/
Both of those modules should already be installed on your system.
Quick example:
my %params = $cgi->Vars;
# if you don't like getting %params from $cgi, you can just list all
# values as you have them and their names in Dump
my $param_dump = Data::Dumper->Dump([\%params],["*params"]);
my $now = localtime;
my $key = md5_hex("$param_dump,$now");
HTH
--
Sean Quinlan <[email protected]>
signature.asc
(application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQBCOdumnv2yYfTgGZsRAnosAJ96FQmDndus4XC/kyy7vQ2ZySkhOwCghdUQ DJEg3wKpxenJhagjNbPENlw= =h9xj -----END PGP SIGNATURE-----