1

How should I check a checkbox like this:

<td><center><INPUT name="test2%40mydomain.com_notmetoo" type="CHECKBOX" value="off" ></center></td>

with Perl's LWP::UserAgent module?

This code doesn't do it (the output is as if it was submitted without the checkbox being checked - i.e. the webpage gets refreshed and the checkbox cleared).

#!/usr/bin/perl
use LWP::UserAgent;

$ua = new LWP::UserAgent;
$ua->cookie_jar({ file => "$ENV{HOME}/.mailmanrc" });

$res = $ua->post('http://mydomain.com/mailman/admin/test1_mydomain.com/members/list',
    Content_Type => 'form-data',
    Content => [
        'test2%40mydomain.com_notmetoo' => 'on',
        setmemberopts_btn => 'Submit Your Changes'
    ]
);
if ($res->is_success) {
    print $res->decoded_content;
    print "Changed user setting...I hope!\n";
}
else {
    die $res->status_line;
}

I'm not sure why the "@" is represented by "%40" in the element name (any ideas?), but I've also tried: 'test2\%40mydomain.com_notmetoo' => 'on', and 'test2@mydomain.com_notmetoo' => 'on', without success.

There seems to be no JavaScript on the website (i.e. it works fine in my browser with JavaScript disabled). I just can't get it to work in Perl.

Thanks.

Terry.


Update #1: Here's the POSTDATA value from Firefox's 'Tamper Data' extension when I submit after checking Test2's notmetoo box:

POSTDATA =-----------------------------2921376274802
Content-Disposition: form-data; name="findmember"


-----------------------------2921376274802
Content-Disposition: form-data; name="test1%40mydomain.com_realname"

Test1
-----------------------------2921376274802
Content-Disposition: form-data; name="user"

test1%40mydomain.com
-----------------------------2921376274802
Content-Disposition: form-data; name="test1%40mydomain.com_hide"

on
-----------------------------2921376274802
Content-Disposition: form-data; name="test1%40mydomain.com_notmetoo"

on
-----------------------------2921376274802
Content-Disposition: form-data; name="test1%40mydomain.com_plain"

on
-----------------------------2921376274802
Content-Disposition: form-data; name="test1%40mydomain.com_language"

en
-----------------------------2921376274802
Content-Disposition: form-data; name="test2%40mydomain.com_realname"

Test2
-----------------------------2921376274802
Content-Disposition: form-data; name="user"

test2%40mydomain.com
-----------------------------2921376274802
Content-Disposition: form-data; name="test2%40mydomain.com_hide"

on
-----------------------------2921376274802
Content-Disposition: form-data; name="test2%40mydomain.com_notmetoo"

off
-----------------------------2921376274802
Content-Disposition: form-data; name="test2%40mydomain.com_plain"

on
-----------------------------2921376274802
Content-Disposition: form-data; name="test2%40mydomain.com_language"

en
-----------------------------2921376274802
Content-Disposition: form-data; name="test3%40mydomain.com_realname"

Test3
-----------------------------2921376274802
Content-Disposition: form-data; name="user"

test3%40mydomain.com
-----------------------------2921376274802
Content-Disposition: form-data; name="test3%40mydomain.com_hide"

on
-----------------------------2921376274802
Content-Disposition: form-data; name="test3%40mydomain.com_plain"

on
-----------------------------2921376274802
Content-Disposition: form-data; name="test3%40mydomain.com_language"

en
-----------------------------2921376274802
Content-Disposition: form-data; name="setmemberopts_btn"

Submit Your Changes
-----------------------------2921376274802
Content-Disposition: form-data; name="allmodbit_val"

0
-----------------------------2921376274802--

Does that help anyone to help me?

Terry
  • 176
  • 9
  • You must show us the entire form that encloses this input. Is this one of a set of check boxes with the same name? – Borodin Aug 15 '12 at 03:15
  • If you are emulating a browser then you should consider WWW::Mechanize – Borodin Aug 15 '12 at 03:21
  • @Borodin. Thanks for your suggestions. Here's a copy of the entire form: (http://homepages.paradise.net.nz/abcd/mailman1.htm). As you can see, the checkboxes have different names. I just noticed that unlike the other webpages in this GNU Mailman "website" (which I'm having no such problems with), this one has 'enctype="multipart/form-data"' in the
    tag. At least some of the others don't have 'enctype...' at all. Does that help us? I'm not trying to emulate a browser - just check a box via Perl.
    – Terry Aug 21 '12 at 10:04

2 Answers2

2

My understanding of checkboxes is that the value= specifies the value which the variable will have when the checkbox is checked. So your confusing value="off" actually means thay you need

'test2%40mydomain.com_notmetoo' => 'off'

To indicate that the box is checked.

Sodved
  • 8,428
  • 2
  • 31
  • 43
  • Good points Sodved. Note that my 1st post gives an example of an 'unchecked' element. Here's 2 such elements from the webpage - one checked and one not: `
    ` Looks as if the checked ones have 'value="on" CHECKED' and the rest just have 'value="off"', so I'm not sure what purpose the 'value' attribute serves in this case. Note: I didn't create this (GNU Mailman) webpage and I can't change it.
    – Terry Aug 21 '12 at 10:22
1

In your case the checkbox will have value "off" when checked. This would be less confusing if value is something like value="checkbox1". In this case the value of the checkbox would be checkbox1 when checked.

Also, consider using WWW::Mechanize for basic web operations like this (without javascript), its much easier and intuitive, also WWW::Mechanize is a subclass of LWP::UserAgent so you could still use LWP's methods.

Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77
  • Thanks Annjawn. I think my checkboxes have value='on' when checked. See my 2 comments above, including the copy of the webpage I linked to in my 1st comment. Aren't I likely to face the same kind of issue if I try to use WWW::Mechanize? – Terry Aug 21 '12 at 10:25
  • @Terry `WWW::Mechanize` is much simpler to use and debug. Also, as I mentioned earlier, you will still be able to use `LWP` methods sing `WWW::mechanize` inherits those. – Anjan Biswas Aug 21 '12 at 13:55
  • Thanks Annjawn. I've started looking at WWW::Mechanize. I've added "Update #1" to my original post, with more info. – Terry Aug 21 '12 at 23:19
  • Hi again Annjawn. As per your suggestion, I tried WWW::Mechanize and checked the boxes like this: `$m->tick('test2%40mydomain.com_notmetoo', 'off');` and unchecked them like this: `$m->untick('test2%40mydomain.com_notmetoo', 'on');` and that seems to work, thank you. It gives me a lot of warnings when the box is already in the state that I'm 'changing' it to (e.g. if I try to check a checked box). Errors are like:`No checkbox "test2%40mydomain.com_notmetoo" for value "on" in form at ./mailman_update.pl line 102`. Any ideas how to avoid them (without having to look at the box state first)? – Terry Aug 29 '12 at 06:44