Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: repeating fields in CGI forms

by Coruscate (Sexton)
on Feb 05, 2003 at 22:21 UTC ( #232959=note: print w/replies, xml ) Need Help??

in reply to repeating fields in CGI forms

While it is possible to use the same field name multiple times ( 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
    Unfortunately, the modules mentioned are the exact reason for my distress. Data::FormValidator and HTML::FillInForm are both wonderful for validating and redisplaying form data. Unfortunately, they don't work well with "subform"ed data.

    And while your code to generate the form looks fine, any templating language is trying to create material that is easy for a non-programmer to edit...and "numbered" fields hurts that considerably.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://232959]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-06-23 03:09 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.