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.
    


In reply to Re: repeating fields in CGI forms by Coruscate
in thread repeating fields in CGI forms by swiftone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.