develooper Front page | perl.libwww | Postings from April 2003

Re: trouble with <SELECT> inputs in HTML::Form

Thread Previous
From:
Mike Simons
Date:
April 3, 2003 09:26
Subject:
Re: trouble with <SELECT> inputs in HTML::Form
Message ID:
20030403172347.GA14069@moria.simons-clan.com
On Tue, Mar 25, 2003 at 08:44:02PM -0800, Ask Bjoern Hansen wrote:
> I can't get <SELECT>'s to work.  Specifically then input->{menu} isn't
> being populated properly, I think.  The dump is okay, but
> ->possible_values doesn't return all the possible values and ->value
> gives me an error if I use anything but the first option.
> 
>   newlines=<UNDEF>                (option)  [*<UNDEF>|x1]
>   newlines=<UNDEF>                (option)  [*<UNDEF>|x2]
>   newlines=<UNDEF>                (option)  [*<UNDEF>|x3]

Ask,

  I got stuck on this for a while when too...

  If you notice the 'option' values are broken into three different 
inputs.  When you run find_input can specify which of them you want 
to find... in fact if you want to set any other than the first you
have to do something like:
===
# get second input with name "newlines"
$input = $form->find_input("newlines", undef, 2);

# set value to non-default, first available value
$input->value(($input->possible_values)[1]);
===

  Would set the second "newlines" variable to the value x2.


> for my $f (qw(newlines nonewlines)) {
>   my $input = $form->find_input($f);
>   print "$f: ", join(" / ", $input->possible_values), "\n";
>   eval { $form->value($f => "x3") };
>   warn $@ if $@;
> }

  The $form->value function only supports setting the first input of a
given name.  If you have multiple items with the same name you have
to use either find_input yourself as above, or $form->inputs (shown
below), to loop over the items to set.

     Good Luck,
       Mike Simons

===
#! /usr/bin/perl -w

use strict;
use HTML::Form;

$/ = undef;

my $form = HTML::Form->parse(<DATA>, 'http://example.com/');
print $form->dump;

for my $input ($form->inputs) {
  print "", $input->name, ": ",
    join(" / ", map { $_ or "undef" } $input->possible_values), "\n";
  eval { $input->value(($input->possible_values)[1]) };
  warn $@ if $@;
}

print "===\nnew values:\n";
print $form->dump;

__DATA__
<form action="/">

<SELECT NAME="newlines" MULTIPLE>
<OPTION VALUE="x1">x1
<OPTION VALUE="x2">x2
<OPTION VALUE="x3">x3
</SELECT>

<SELECT NAME="nonewlines" MULTIPLE>
<OPTION VALUE="x1">x1<OPTION VALUE="x2">x2<OPTION VALUE="x3">x3
</SELECT>

</form>

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About