http://www.perlmonks.org?node_id=957699

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

Using Mechanize, I am trying to pass a name/value pair to a form that looks like this:

<form action="action.cgi?use-a" method="post" > <input type="submit" value="entry one" >&nbsp;for&nbsp; <select name="target"> <option value="">User <option value='195'>Affiliate </select> </form>

Other variants of action.cgi work fine for me, e.g. action.cgi?use-f, but those variants don't require a name value/pair to work. I simply do a $browser->get() on them. If I simply get action.cgi?use-a, the cgi script acts as if I had selected the User option, since it received no name/value pair.

I have tried many ways of structuring the url directly but without any success. I have also tried using LWP's post method, without success, like this:

$browser->post("$BASEURL?use-a", 'target' => '195');

I am stumped (and probably stupid.) Would you be kind enough to help?

Replies are listed 'Best First'.
Re: Using Mechanize With Odd Query String
by repellent (Priest) on Mar 03, 2012 at 21:12 UTC
    Your form key/value pairs need to be specified as Content:
    $browser->post("$BASEURL?use-a", { 'target' => '195' } ); # now a hash +ref

    The documentation for ->post mentions $field_name which may be confused as being a form key name. The $field_name => $value pairs actually get pushed onto the Header instead of the Content.
      Thank you. I did not understand the documentation even when I re-read it. Thank you for explaining it succinctly.
Re: Using Mechanize With Odd Query String
by Gangabass (Vicar) on Mar 05, 2012 at 09:33 UTC
    Just load this form's page with $mech->get(...) and do:
    $mech->submit_form( form_number => 1, fields => { target => "Affiliate", }, );
    But you need to check form_number param by yourself.