Automatic HTML form submission using WWW::Mechanize

Here is a short tip on how to automatically submit a HTML form using a Linux command line and perl script. For this example we would need a WWW::Mechanize perl module and some basic PHP website. Let’s start with simple PHP website. The website will consist of two files:


<form action="submit.php" method="post">
First Name: <input name="fname" type="text" />
Last Name: <input name="lname" type="text" />
<input type="submit" />


First Name: <?php echo $_POST["fname"]; ?><br />
Last Name: <?php echo $_POST["lname"]; ?>

Upload those two files to your webserver’s directory and change their permissions:

chmod 755 form.php submit.php

If you have not done so yet, install WWW::Mechanize. On Debian or Ubuntu it would be something like this:

# apt-get install libwww-mechanize-perl

and create a script called with a following content:


use WWW::Mechanize;
my $mech = WWW::Mechanize->new();

$url = 'http://localhost/form.php';
$mech->get( $url );

        form_number => 1,
        fields      => {
            fname    => 'www',
            lname    => 'mechanize',

print $mech->content();

Note the URL in the above script. Edit this URL to fit your settings. Make the script executable:

$ chmod +x

Now execute this script and redirect all output to index.html

./ > index.html

If everything was OK open up index.html using your browser and you should see:

First Name: www
Last Name: mechanize