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

I've been trying to better understand how Mechanize works (see this link) and the next obstacle I've encountered is submitting forms with hidden fields. The following code fails, and I suspect it has something to do with the three fields listed that are hidden in the form.
use warnings; use strict; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); my $id = '14632'; my $url = "http://home.myspace.com/index.cfm?". "fuseaction=user.viewfriends&". "friendID=" . $id; $mech->get( $url ); my $page = 2; my ($viewstate) = $mech->content =~ m/name="__VIEWSTATE".+value="([^"] ++)"/io; my $form = $mech->form_name('aspnetForm'); $form->value('__EVENTTARGET' => 'ctl00$cpMain$pagerTop'); $form->value('__EVENTARGUMENT' => $page); $form->value('__VIEWSTATE' => $viewstate); $mech->submit(); print $mech->content;
If someone has time, would you mind telling me what I'm doing wrong or missing?

Thanks

Replies are listed 'Best First'.
Re: How to submit hidden forms
by Cody Pendant (Prior) on Jan 31, 2007 at 04:11 UTC
    Well I have two problems:
    1. the id you gave us is private so I can't get the page without a MySpace logon
    2. When I use it on a non-private URL, it dies with a very helpful error message saying that there's no field called __EVENTTARGET.

    So, what did it do for you?

    Edited because there is a hidden field with that name. I guess that's the problem.

    I don't set form fields that way, I just do

    $mech->field( 'name', 'value' );
    and it seems to work.


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: How to submit hidden forms
by Herkum (Parson) on Jan 31, 2007 at 04:50 UTC

    Are their multiple forms on this web page?

    Are their multiple inputs of the same name on the page?

    Do those form fields really exist on the page?

    I don't an answer for you, I am just giving you some suggestions on where to look for some potential problems.

Re: How to submit hidden forms
by CountZero (Bishop) on Jan 31, 2007 at 05:55 UTC
    It would be helpful if you could show us the relevant details of the form on that web-page, so we can try ourselves.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How to submit hidden forms
by andyford (Curate) on Jan 31, 2007 at 17:42 UTC

    As I pointed out here, you could look at the code in WWW::Myspace or just use the module. A recent update to version 0.62 should have things working again, even though 0.61 is broken.

    non-Perl: Andy Ford

Re: How to submit hidden forms
by lv211 (Beadle) on Feb 01, 2007 at 20:07 UTC
    Here's what I would do.

    1. Download the page with the form you want to your computer.

    2. Delete the part in the form where it says action="example.php (or whatever)" and put in "formpairs.pl (saved in the same location or whatever)". The script below is formpairs.pl.

    #!/usr/bin/perl # formpairs.pl - extract names and values from HTTP requests use strict; use warnings; my $data; if(! $ENV{'REQUEST_METHOD'}) { # not run as a CGI die "Usage: $0 \"url\"\n" unless $ARGV[0]; $data = $ARGV[0]; $data = $1 if $data =~ s/^\w+\:.*?\?(.+)//; print "Data from that URL:\n(\n"; } elsif($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN, $data, $ENV{'CONTENT_LENGTH'}); print "Content-type: text/plain\n\nPOST data:\n(\n"; } else { $data = $ENV{'QUERY_STRING'}; print "Content-type: text/plain\n\nGET data:\n(\n"; } for (split '&', $data, -1) { # Assumes proper URLencoded input tr/+/ /; s/"/\\"/g; s/=/\" => \"/; s/%20/ /g; s/%/\\x/g; # so %0d => \x0d print " \"$_\",\n"; } print ")\n"; __END__

    3. Open the page up in your browser locally, and then run the code by submitting the form.

    4. This should show you what values you need in your form.

    Also check out Perl & LWP by Sean Burke. That's where this code was taken from.

    From there I use $mech->set_fields(); to input the form info.

    Have fun spamming Myspace! ;)
      Very nice, Thanks!
Re: How to submit hidden forms
by Anonymous Monk on Jan 31, 2007 at 07:34 UTC
    If someone has time, would you mind telling me what I'm doing wrong or missing?
    You're not trying hard enough.