Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

retrieve dynamic form data

by Anonymous Monk
on Nov 22, 2005 at 21:06 UTC ( #510905=perlquestion: print w/replies, xml ) Need Help??

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

I have a form that is dynamic with a particular qt# field. There can be infinitely many qty fields with a number. For example, qty2, qty3, qty803.

The numbers aren't necessarily in any order and the numbers can skip around a lot (not all numbers will be there). These are the ONLY form fields minus the submit button. My question is, how can I retrieve an unknown number of qty fields? I've never done anything with forms where I didn't know what fields I was accepting.

Thank you.

Replies are listed 'Best First'.
Re: retrieve dynamic form data
by ercparker (Hermit) on Nov 22, 2005 at 23:24 UTC
    you can try something like this:
    use CGI; my $q = new CGI; my %input = $q->Vars; foreach my $key (keys %input) { if ($key =~ m{\Aqty\d+}) { # DO SOMETHING } }
    hope that helps
Re: retrieve dynamic form data
by cbrandtbuffalo (Deacon) on Nov 22, 2005 at 21:13 UTC
    Assuming you're using CGI, you can use the param method to return all of the form parameters passed in. One of the nice things about param is that you can use it in list context, so you can loop through or grab the names of all params, so you can do:
    my @names = CGI::param; for my $name ( @names ){ # Get each value. my $value = CGI::param($name); # Do something with the value. }
      I have tried doing this but the array always comes back empty. Any idea why?
        I don't know for sure, but the simple reason would be that no parameters are being passed to the script. You could test this, even just to rule it out, by putting a hidden field in your form with a known value or even putting some query params at the end of the 'action' line in your form tag. I wouldn't recommend doing this permanently, but it can help debug. For example, in your form, you could:
        <form action="">
        How are you testing whether the params are coming back? Just printing back to the browser? Because it's also possible that you are getting them, but you aren't displaying them properly. Can you show some code?
Re: retrieve dynamic form data
by philcrow (Priest) on Nov 22, 2005 at 21:17 UTC
    There are lots of ways to do this. Usually your helper (like will provide the parameters as a hash if you ask nicely. The details depend on what helper you use. In CGI you could say:
    use CGI ’:cgi-lib’; $params = Vars;
    This returns a hash ref. (mod_perl, which I use, has a similar scheme.)

    Then you can simply delete the submit key from that hash (unless it's read only, then make a copy first). Finally, loop through the keys of the hash.


      actually it returns a hash not hash ref so: %params = Vars; #or using OO method $cgi = new CGI; %params = $cgi->Vars;

      "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      I tried this but it failed under strict. I used my $var = Vars(); to see if that worked, and it didn't bring values back into a hash %var.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://510905]
Front-paged by kutsu
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2020-07-04 18:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found