Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

reading dynamic form params

by Anonymous Monk
on Dec 12, 2006 at 19:02 UTC ( [id://589361]=perlquestion: print w/replies, xml ) Need Help??

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

I am populating an HTML form with fields pulled from a database. This means that depending on many different factors, different form fields will be placed on screen that I need to read from after it's submitted.

Since the fields are always dynamic, I'm not exactly sure how to physically take the data from the form and put it back into the database.

I set up the variables to print out the text fields based off the name pulled from the DB, -, and value1-4 (not all sets of fields have all 4 possible value fields, some just have 1 or 2)

<input type="$stored_name-value1"> <input type="$stored_name-value2"> <input type="$stored_name-value3"> <input type="$stored_name-value4"> # assuming $stored_name is a NEW name and only 2 fields <input type="$stored_name-value1"> <input type="$stored_name-value2">
The thing is, there's no "common" thing between my infinite number of different name-value1-4 pairs.

How on earth can I read in the input after they submit the form?

I think a hash would be the way to do it, but I have no idea how I could get un-like data together or what I could do to make the data similar but to the point I can break it apart later.

Replies are listed 'Best First'.
Re: reading dynamic form params
by wfsp (Abbot) on Dec 12, 2006 at 19:28 UTC
    This might give you a pointer. It uses HTML::Template

    The next stage is that DBI can be coaxed into providing just this sort of structure. See HTML::Template nested loops, DBI/MySQL and map for a discussion.

    You could store the data structure in a CGI::Session. So when you get the form back you will know what it contains.

    Hope that helps.

    #!/usr/bin/perl use strict; use warnings; use HTML::Template; my $rec = [ { name => 'field1', value => 'one', }, { name => 'field2', value => 'two', }, { name => 'field3', value => 'three', }, { name => 'field4', value => 'four', }, ]; my $t = HTML::Template->new(filehandle => *DATA); $t->param(rec => $rec); print $t->output; __DATA__ <form> <TMPL_LOOP NAME="rec"> <input TYPE="text" NAME="<TMPL_VAR NAME="name">" VALUE="<TMPL_VAR NAME="value">" /> </TMPL_LOOP> </form>
    output:
    ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl <form> <input TYPE="text" NAME="field1" VALUE="one" /> <input TYPE="text" NAME="field2" VALUE="two" /> <input TYPE="text" NAME="field3" VALUE="three" /> <input TYPE="text" NAME="field4" VALUE="four" /> </form > Terminated with exit code 0.
Re: reading dynamic form params
by sulfericacid (Deacon) on Dec 12, 2006 at 19:13 UTC
    Tested and works but there's probably a better solution.
    my @names = param(); my %data; foreach my $name (@names) { next if ($name =~ m/SubmitButtonName/); $data{$name} = param($name);; } foreach my $key (keys %data) { print "$key => $data{$key}<br>\n"; } # all goodies are in %data
    UPDATE: I decided to go ahead and try to load the hash to the param directly thinking perhaps there's a chance, but it failed. It stored just the params and not the values.
    #!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; use CGI::Carp 'fatalsToBrowser'; print header, start_html; my %data = param(); foreach my $key (keys %data) { print "$key => $data{$key}<br>\n"; }


    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

Log In?
Username:
Password:

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

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

    No recent polls found