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

What is the right to select some checkboxes using WWW::Mechanize? The page looks like this
...snip... <input type="checkbox" value="5100" name="buscodes" >5100 Industrial M +anufacturers<br> <input type="checkbox" value="5200" name="buscodes" >5200 Industrial W + & D<br> <input type="checkbox" value="5210" name="buscodes" >5210 Internet Dis +tributors<br> <input type="checkbox" value="5300" name="buscodes" >5300 Business Equ +ipment M & D<br> ...snip...
I was trying to use code like this without success
foreach my $val (qw(2800 2820 2830 2840 2850)) { $m->field('buscodes', $val); }
Thanks --

rkg

Replies are listed 'Best First'.
(jeffa) Re: WWW::Mechanize and checkboxes
by jeffa (Bishop) on Apr 23, 2003 at 23:43 UTC
    You need to specify (uggghh!) the optional $number argument in the field() method:
    
       The optional "$number" parameter is used to distinguish
       between two fields with the same name.  The fields are
       numbered from 1.
    
    Here is some code to test with. First, the CGI script:
    use strict; use warnings; use CGI qw(:standard); print header, start_form, 'Enter something: ', textfield('text1'), checkbox_group( -name => 'checkbox1', -values => [qw(foo bar baz qux)], ), submit('go'), end_form, ; if (param('go')) { print pre(map "$_: @{[param($_)]} \n", param); }
    And the Bot to query it with:
    use strict; use warnings; use WWW::Mechanize; my $agent = WWW::Mechanize->new(); $agent->get('http://localhost/cgi-bin/foo.cgi'); $agent->form(1); $agent->field(qw(text1 HELO)); $agent->field(qw(checkbox1 foo 1)); $agent->field(qw(checkbox1 baz 3)); $agent->click('go'); print ($agent->{content} =~ /<pre>(.*)<\/pre>/s);
    Hope this helps. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)