Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

retrieve dynamic form data

by Anonymous Monk
on Nov 22, 2005 at 21:06 UTC ( [id://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="http://my.site.com/script.pl?test1=one">
        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 CGI.pm) 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.

    Phil

      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found