We help IT Professionals succeed at work.
Get Started

Posting to a form using Perl's LWP::Agent

hopkimi asked
Last Modified: 2013-12-25
I want to fill out a form on a web app using Perl and LWP::UserAgent.

If the form has multiple check boxes, and I only want to change one of them, do I have to post all of the fields to the form or can I just submit the one field that I want to change?

For example, in this form, all three items are presented as checked:  milk, butter, and cheese.  

<title>My Page</title>
<form name="myform" action="/myformhandler.cgi" method="POST">
<div align="left"><br>
<input type="checkbox" name="option1" value="Milk" checked> Milk<br>
<input type="checkbox" name="option2" value="Butter" checked> Butter<br>
<input type="checkbox" name="option3" value="Cheese" checked> Cheese<br>

<input type="submit" value=submit>

If I want to submit the form with cheese unchecked, can I do this?

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla 8.0");
use HTTP::Request::Common qw(POST);
my $req = (POST 'http://perl.noobs.com/myform.cgi',
       [ option3 => 'off',
my $request = $ua->request($req);

Or do I have to submit all form items like this?

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("Mozilla 8.0");
use HTTP::Request::Common qw(POST);
my $req = (POST 'http://perl.noobs.com/myform.cgi',
       [ option1 => 'on',
         option2 => 'on',
         option3 => 'off'
my $request = $ua->request($req);

Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE