Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to access an unknown number of CGI variables in a CGI script

by prautt (Initiate)
on Jan 08, 2013 at 08:37 UTC ( #1012191=perlquestion: print w/ replies, xml ) Need Help??
prautt has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have an HTML form in which a user can click on "Add a row" link to have another row (and another variable) added to the form using JavaScript. I name each new variable "student[]" in the form.

The question is how to access these variables in a Perl CGI script? Should I be naming them differently in the form to access them as, for example, $FORM{'student1'}, $FORM['student2'}, etc.? Or perhaps there's a better way to name the variable in the form to begin with? Thanks!

Comment on How to access an unknown number of CGI variables in a CGI script
Re: How to access an unknown number of CGI variables in a CGI script
by Anonymous Monk on Jan 08, 2013 at 09:02 UTC

    The name doesn't need to change

    Use CGI/CGI::Simple.. param method, it will return a list of each row you added

    or use Plack::Request body_parameters , its returns a Hash::MultiValue, something you might build using CGI yourself

      The name doesn't need to change (...) param method, it will return a list of each row you added

      Demonstration:

      > perl -MCGI=param -E ' say "student=$_" for param("student"); ' student=foo student=bar student=baz student=foo student=bar student=baz >

      Note that this is similar to space_monk's functional style example below.

Re: How to access an unknown number of CGI variables in a CGI script
by 7stud (Deacon) on Jan 08, 2013 at 09:02 UTC

    Hello, I have an HTML form in which a user can click on "Add a row" link to have another row

    There's no such thing as a row in an html form.

    Or perhaps there's a better way to name the variable in the form to begin with? Thanks!

    HTML forms do not have variables.

      Or perhaps there's a better way to name the variable in the form to begin with? Thanks!
      "student"

      Maybe have been thinking of "php form arrays" in which case CGI::Struct

      Hello, I have an HTML form in which a user can click on "Add a row" link to have another row
      There's no such thing as a row in an html form.
      Sure there is
      Or perhaps there's a better way to name the variable in the form to begin with? Thanks!
      HTML forms do not have variables.
      Sure they do
Re: How to access an unknown number of CGI variables in a CGI script
by space_monk (Chaplain) on Jan 08, 2013 at 09:56 UTC
    Here you go...
    use strict; use warnings; use CGI; # do some other stuff..... # either functional style... my @students = param('student[]'); # OR OO-style... my $query = CGI->new; my @students = $query->param('student[]'); # OR Gangnam-style..... # no just kidding :-) # now process your student input, # remember to check no one is sending you weird data ..... foreach my $student (@students) { # wibble }
    A Monk aims to give answers to those who have none, and to learn from those who know more.
      Thanks much! Is it possible to do it without using CGI.pm?

        Thanks much! Is it possible to do it without using CGI.pm?

        Sure, why do you ask?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012191]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-07-23 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (154 votes), past polls