Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found