Works like a charm, thanks! Guess I was trying to make it to difficult. I
must've done something wrong, though... I had to alter the code a bit to
make it work. It seemed that grabbing all the params tossed the param values
and I had to go back and get 'em once I had the key isolated.
Naming the fields as you suggested:
<textarea name="question_41" rows="6" cols="60">How's this gonna
work?</textarea><br>
<textarea name="question_40" rows="6" cols="60">Still another sample
question</textarea><br>
<textarea name="question_44" rows="6" cols="60">Here's a "swell" performance
question!</textarea>
And scripting thusly:
my $req = new CGI;
my @URL_VARS = $req->param;
foreach my $VARNAME (@URL_VARS) {
if($VARNAME =~ /^question_(\d+)$/) {
my $st = "UPDATE tablename SET answer = '" . $req->param($VARNAME)
. "' WHERE id = '$1'";
print $st . "<br>\n";
}
}
Gave me:
UPDATE tablename SET answer = 'How's this gonna work?' WHERE id = '41'
UPDATE tablename SET answer = 'Still another sample question' WHERE id =
'40'
UPDATE tablename SET answer = 'Here's a "swell" performance question!' WHERE
id = '44'
> -----Original Message-----
> From: Wiggins d Anconia [mailto:[EMAIL PROTECTED]
> Sent: Friday, November 14, 2003 2:19 PM
> To: Mayo, Chuck; '[EMAIL PROTECTED]'
> Subject: Re: Form field named as array elements
>
>
> >
> > Hi all,
> >
> > I need to present a series of database fields for users to
> update and when
> > the form is submitted I need to be able to retrieve not
> only the field
> > values but the original row ID's as well so I'll know which
> table rows to
> > update.
> >
> > What I'd like to be able do is name the form fields as
> array elements:
> >
> > <form>
> > <input type="text" name="question{'25'}" value="$answer{'25'}">
> > <input type="text" name="question{'47'}" value="$answer{'47'}">
> > </form>
> >
> > Then when the form came back in, create update statements from the
> key/value
> > pairs.
> >
> > while (($key,$value) = each %question) {
> > $query = "UPDATE tablename SET answer = '$value'
> WHERE id =
> '$key'
> > ";
> > }
> >
> > Does that make any sense at all? Can someone tell me how
> I'd do that in
> > PERL?
> >
>
> It makes sense and your solution is close. Rather than using difficult
> names in your input tags just use simplified names, as just submitting
> the form won't make your %question spring into life automagically. So
> given something like:
>
> <input type=text name="question_45" value="$answer{'45'}">
> <input type=text name="question_27" value="$answer{'27'}">
>
> Then the answers get interpolated in and/or updated by the client end.
> Then when they submit you will look for something like:
>
> my %params = $req->params;
>
> while (my ($key, $val) = each (%params)) {
> if ($key =~ /^question_(\d+)$/) {
> my $question = $1;
> my $st = "UPDATE tablename SET answer='$val' WHERE
> id=$question";
> ...
> }
> }
>
> This steps through the list of all input parameters, as the
> names should
> be unique, looking for a particular criteria, in this case the string
> 'question_' followed by a set of digits that we know correspond to the
> ids of the questions. By using the (\d+) we capture that idea into $1,
> which I then set to $question to make it easy to read. The value
> associated with that key is your answer. I tried to make
> this verbose,
> there is shorter typing that would get the job done and I
> would suggest
> using answer=? with a binding value to avoid single quote
> snafu's, etc.
> but you get the general idea.
>
> It's 'Perl' or 'perl' depending on whether you are talking about the
> language or the interpreter, but not PERL....
>
> http://danconia.org
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]