Re: Parsing a form with identical field names
[email protected] (Father Chrysostomos) Sun, 17 Jul 2011 13:32:13 -0700
| Newsgroups | perl.libwww |
|---|---|
| Message-ID | <[email protected]> |
On Jul 16, 2011, at 9:30 PM, David White wrote:
> On further investigation, it might just be a limit (bug?) of
> WWW::Mechanize. Consider this form:
>
> <input type="text" name="myfield" value="One">
> <input type="text" name="myfield" value="Two">
> <input type="text" name="myfield" value="Three">
>
> and this Perl snippet:
>
> my $mech = WWW::Mechanize->new();
> $mech->get('http://www.example.com/form.html');
> $mech->form_with_fields('myfield');
> $mech->click();
>
> The value for "myfield" passed to the form handler will contain all 3
> values (e.g. an array of "One","Two","Three" in Perl, PHP, etc.)
>
> Now consider this form:
>
> <select name="myfield"><option value="One">One</option></select>
> <select name="myfield"><option value="Two">Two</option></select>
> <select name="myfield"><option value="Three">Three</option></select>
>
> If this form is submitted by a browser (I tried FF3.6 and IE8), the
> form handler will again receive an "myfield" array of
> "One","Two","Three". If submitted via WWW::Mechanize, however, the
> handler will receive only a value of "One".
>
> A bug, perhaps?
>
> Incidentally, if the form is rewritten this way, with each box having
> its first (and only) item selected:
>
> <select name="myfield"><option value="One" selected>One</option></
> select>
> <select name="myfield"><option value="Two" selected>Two</option></
> select>
> <select name="myfield"><option value="Three" selected>Three</
> option></select>
>
> FF3.6 and IE8 will return the same results ("One","Two","Three"), but
> WWW::Mechanize will now send a "myfield" value of "Three" to the form
> handler.
I just tried that out with WWW::Scripter (a Mech subclass), and it doesn’t have that problem. It uses a different module (HTML::DOM) for form serialisation. Mech uses HTML::Form. Maybe it’s an HTML::Form bug (cc'ing the LWP list).
>
> On Jul 16, 7:44 pm, David White <[email protected]> wrote:
>> Andy, thanks for the reply. There's no JavaScript involved - rather,
>> I think Koha is relying on a feature of CGI.pm usually intended to
>> handle <select multiple> boxes.
>>
>> For example, this form:
>>
>> <select name="foo"><option value="One">One</option></select><br>
>> <select name="foo"><option value="Two">Three</option></select><br>
>> <select name="foo"><option value="Three">Three</option></select><br>
>>
>> processed with this Perl code:
>>
>> use CGI qw(:all);
>> my $q = new CGI;
>> my @boxvals = $q->param('foo');
>> foreach my $boxval (@boxvals) {
>> print "A select box value was '$boxval'<br>\n";
>> }
>>
>> will generate:
>>
>> A select box value was 'One'
>> A select box value was 'Two'
>> A select box value was 'Three'
>>
>> So far so good. The question is then how to access, say, the second
>> <select> box and set its value while leaving the rest of them alone.
>>
>> Thanks again.
>>
>> On Jul 16, 7:10 pm, Andy Lester <[email protected]> wrote:
>>
>>> On Jul 15, 2011, at 11:14 PM, David White wrote:
>>
>>>> My goal is to pick a particular <select> box (say, the third one),
>>>> select the 7th option in its list, and submit the form. However with
>>>> the <select> boxes having the same name (albeit different "id" tags),
>>>> I cannot figure out how to specify to WWW::Mechanize the precise box
>>>> that I want.
You could set it this way:
# Here $mech is a WWW::Scripter object.
$mech->current_form->{$sel_name}[2]->options->[6]->selected(1);
If that doesn’t work, try this:
(
$mech->current_form->look_down( # an HTML::Element method
_tag => 'select',
name => $sel_name,
)
)[2]->options->[6]->selected(1);