in reply to Strategy to name/process many similar form fields with cgi.pm?

Until recently I have used the strategy that you have shown above. I would now suggest using Data::URIEncode.

You would begin by using a form that looks similar to the following:

<form ...> <table> <tr> <td><b>Username 1</b></td> <td><input type=text name="users:0.username"></td> </tr> <tr> <td><b>Email</b></td> <td><input type=text name="users:0.email"></td> </tr> <tr> <td><b>Name</b></td> <td><input type=text name="users:0.name"></td> </tr> </table> <table> <tr> <td><b>Username 1</b></td> <td><input type=text name="users:1.username"></td> </tr> <tr> <td><b>Email</b></td> <td><input type=text name="users:1.email"></td> </tr> <tr> <td><b>Name</b></td> <td><input type=text name="users:1.name"></td> </tr> </table> </form>


So far so easy. You can use pretty much whatever templating tools you want for generating the html - clear from hard coded html (don't do that), to CGI html methods, to Template::Toolkit (I'd suggest doing that).

When the form is posted to cgi script, just pass the CGI query through Data::URIEncode.

use CGI; use Data::URIEncode qw(query_to_complex); use Data::Dumper qw(Dumper); my $q = CGI->new; my $data = query_to_complex($q); print "Content-type: text/plain\n\n"; print Dumper $data; __END__ Will have printed something similar to: $VAR1 = { users => [{ username => "...", email => "...", name => "...", },{ username => "...", email => "...", name => "...", }], };


You'll still need to do all of the validation, but getting the records into the right structure is already done.

my @a=qw(random brilliant braindead); print $a[rand(@a)];

Replies are listed 'Best First'.
Re^2: Strategy to name/process many similar form fields with cgi.pm?
by punch_card_don (Curate) on Apr 06, 2007 at 00:35 UTC
    Now THAT's the kind of thing I suspected was out there, if someone would just ask. Very nice. Thanks.




    Forget that fear of gravity,
    Get a little savagery in your life.