Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Need to retrieve data passed from dynamically created radio buttons.

by Seumas (Curate)
on Sep 19, 2001 at 23:02 UTC ( [id://113446]=perlquestion: print w/replies, xml ) Need Help??

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

I pull all user records from an SQL database to produce the following form. This form displays radio buttons to choose a new account status and lists the current account status and the user's name. My desire is to then process the new status settings after the form is submitted and write these changes to the database. The problem I'm encountering is that, because these radio buttons are dynamically created, I can not figure out how to pull them through cgi.pm for use in the next step.

Here is an example of the form:

A( )  N( )  Y( ) --- N John Doe 
A( )  N( )  Y( ) --- Y Jane Smith


The code that generates the form is as follows.
(I should and will eventually use cgi.pm to produce the radio buttons).

foreach my $user_record (@$user_records) { my ($username, $status) = @$user_record; print qq~ <input type="radio" name="$username" value="set_admin">A <input type="radio" name="$username" value="set_not_activated">N <input type="radio" name="$username" value="set_activated">Y --- [$status] $username<br> ~; }

The problem is that after the form is submitted, I need to be able to grab the data for each user from the form and the status for each of those users and then write those changes to the database. Obviously, the number of records will vary each time and the name element of each radio button will be different (actually, there will be three for each user since there will have to be three radio buttons for each user).

I haven't found any other examples of this, but I'm still looking. It seems like the solution is at the tip of my tongue, but I'm not quite there.

  • Comment on Need to retrieve data passed from dynamically created radio buttons.
  • Download Code

Replies are listed 'Best First'.
Re: Need to retrieve data passed from dynamically created radio buttons.
by dvergin (Monsignor) on Sep 19, 2001 at 23:23 UTC
    You can retrieve a list of all the param keys (using CGI.pm in OO style) with:      @names = $query->param and then cycle through the values with something like:
    foreach $name (@names) { next unless is_a_dynamic_item($name); # or whatever my $val = $query->param($name); # do stuff }
      Another way to do it would be to write a hidden field that is, in essence, a data constructor:
      <INPUT TYPE="hidden" name="user_list" value="([dynamic list here])"> and then in the Perl program, use param('user_list') to construct your + array #! Perl -w use strict; use CGI; $my q = CGI->new; my @users = scalar $q->param('user_list'); &do_stuff($_) for (@users); # or whatever
Re: Need to retrieve data passed from dynamically created radio buttons.
by suaveant (Parson) on Sep 19, 2001 at 23:59 UTC
    what I usually do in a case like this is prefix the dynamic fields in question with something I can easily grep out of the param list... like cb_001_foo cb_002_bar or something like that... then you can just grep through the keys...

                    - Ant
                    - Some of my best work - Fish Dinner

Re: Need to retrieve data passed from dynamically created radio buttons.
by Moonie (Friar) on Sep 19, 2001 at 23:17 UTC

Log In?
Username:
Password:

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

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

    No recent polls found