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

I'd like to be able to update multiple database rows on one web page.

If I give the fields the same name, then Apache::Request kindly returns the data to me as an array, so I can do something like this:

my @field1 = $r->param('field1'); my @field2 = $r->param('field2); my @field3 = $r->param('field3');

Although this isn't pretty, I can then iterate over the first array, and grab the value out of the other arrays with the same index number.

The problem is, when I have a field that's a checkbox. If the box isn't checked, then I get no value in the array, and the index numbers don't line up.

I suspect there's a better way of doing this, but if not, is there a way around the problem with checkboxes?

Thanks.

Replies are listed 'Best First'.
Re: Multiple rows in web forms
by ikegami (Patriarch) on Sep 29, 2004 at 14:05 UTC

    For checkboxes, you could use a hash, since each checkbox has a different value:

    my %field1 = map { $_ => $_; } @{$r->param('field1')}; my %field2 = map { $_ => $_; } @{$r->param('field2')}; my %field3 = map { $_ => $_; } @{$r->param('field3')};

    You can check the hashes directrly ($field1{'red'}) or you can then line them up in an array:

    my @array_pos = qw( black blue red ); my @field1 = @field1{@array_pos}; my @field2 = @field2{@array_pos}; my @field3 = @field3{@array_pos};

    This also handles browsers which return the params in random order, which I'm pretty sure is legal.

    Test code:

      Thanks... that worked! :)