in reply to repeating fields in CGI forms
While it is possible to use the same field name multiple times (CGI.pm for example will properly read the data in correctly), I think it would be odd to handle the data. With the same field names, you'd end up with two arrays in your example (one for names, one for authors) or if you wanted to get fancy, you could create an AoH or an AoA.
Myself, I simply prefer using the number idea. It's simple to implement and simple to read the data in. Now, I don't use any of those modules you've mentioned, so I don't know if that increases difficulty for this or not, but here's some example code (untested):
#!/usr/bin/perl -wT use strict; use CGI qw/:standard :cgi-lib *table/; my $max_fields = 10; # Number of times to repeat fields my $input = Vars; if ($input->{'q'} eq 'save data') { open my $fh, '>>', 'data.txt'; for (1 .. $max_fields) { next unless $input->{'title_' . $_} && $input->{'author_' . $_}; print $fh "$input->{'title_' . $_}|$input->{'author_' . $_}\n"; } close $fh; } print header(), start_html('My Data'), start_form(), start_table(), Tr( th( {align=>'left'}, 'Title' ), th( {align=>'left'}, 'Author' ) ); print Tr( td( textfield( {name=>"title_$_",size=>30,force=>1} ) ), td( textfield( {name=>"author_$_",size=>30,force=>1} ) ) ) for 1 .. $max_fields; print Tr( th( {colspan=>2}, submit( {name=>'q',value=>'save data'} ) ) ), end_table(), end_form(), end_html();
C:\>shutdown -s
>> Could not shut down computer:
>> Microsoft is logged in remotely.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: repeating fields in CGI forms
by swiftone (Curate) on Feb 06, 2003 at 15:55 UTC |