monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Most revered brother monks,

I'm trying to create a multiple fields based on user input under CGI.pm. My question are: I'm stuck with my script below:
#!/usr/bin/perl -w use strict; use diagnostics; use Data::Dumper; use CGI qw/:standard :html3/; use CGI::Carp qw( fatalsToBrowser ); my $nbr_of_fields = 15; # This is taken as user input print start_form(),br; for (0..$nbr_of_fields-1) { # This doesn't print print textfield(-name=>'field_name', -value=>'starting value', -size=>50, -maxlength=>80); } print submit( -name=>'action', -value => 'Submit' ), end_form; # And how can I later capture the values?
Update: Corrections added thanks to friedo

Regards,
Edward
  • Comment on Creating multiple textfields with for loops and capturing its values with CGI.pm
  • Download Code

Replies are listed 'Best First'.
Re: Creating multiple textfields with for loops and capturing its values with CGI.pm
by Samy_rio (Vicar) on Jun 15, 2006 at 04:08 UTC

    Hi monkfan, Get the User input and do as below:

    #!C:\perl\bin\perl use strict; use diagnostics; use Data::Dumper; use CGI qw/:standard :html3/; use CGI::Carp qw( fatalsToBrowser ); my $nbr_of_fields = 15; # This is taken as user input print header("text/html"); print start_html(); print start_form({-method=>"post", -action=>"test.cgi"}),br; for (0..$nbr_of_fields - 1) { print textfield(-name=>"field_name$_", -value=>5, -size=>50, -maxlength=>80); } print hidden({-name=>"User_Input", -value=>"$nbr_of_fields"}); print submit({-name=>'action', -value => 'Submit' }), end_form, end_html;

    Fetch the Fields value as follow:

    Test.cgi -------- #!C:\perl\bin\perl use strict; use diagnostics; use Data::Dumper; use CGI qw/:standard :html3/; use CGI::Carp qw( fatalsToBrowser ); my $user_input = param('User_Input'); print header("text/html"); print start_html(); for (0..$user_input - 1) { print "Filed_name$_ :   ", param("field_name$_") +,br; } print end_html;

    I think this will help you.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: Creating multiple textfields with for loops and capturing its values with CGI.pm
by friedo (Prior) on Jun 15, 2006 at 03:55 UTC
    Your code doesn't compile. You're missing a semicolon after the br.

    If the textfields still aren't being printed, it's probably because your loop isn't being executed. Check to make sure your input is what you think it is.

    You're also missing a print before the submit().