steve borruso wrote:
> 
> I don't understand how to manipulate the state of a checkbox.
> I can query the name, type, value, etc. of the input.
> 
> I based my attempts of turning on a checkbox
> on the following append which indicates
>   $f->value(name => undef); would turn the input "OFF".
> 
> http://www.ics.uci.edu/pub/websoft/libwww-perl/archive/2000h2/0485.html
> 
> When I attempt to turn the input "ON"  with
> $f->value($name,defined);  the state does not change. No error
> is given when executed. It doesn't seem to matter what I use in
> place of "defined". It doesn't appear that I'm changing the
> value of the input (as opposed to the state) with what I use either.
> 
> I've studied the HTML::Form doc along with the Perldoc and still
> can't seem to get it right.
> 
> Can someone please take a look at the code sample below
> and point out the logic error and/or provide another example that
> might help.
> 
> Thanks in advance !
> Steve
> 
> boiled down sample of my code ....
> 
> @forms = HTML::Form->parse($html,'http://myurl.com');
> 
> @all_input =  $forms[2] -> inputs;
> 
> for($i=1; $i < $#all_input + 1; $i += 1) {
> 
>    $type=   $all_input[$i]->type;
>    $value = $all_input[$i]->value();
>    $name =  $all_input[$i]->name();
> 
>      if (defined $all_input[$i]->value)   {
>         print "<br> it is on";
>       }
>       else {
>             print "<br> turning it on <br>";
>             $forms[2]->value($name,defined);
>             if (defined $all_input[$i]->value)   {
>                print "<br> STATE SHOULD NOW BE CHECKED (ON)";
>             }
>       }
> }
> 

I don't know what your form looks like.
Try this, it seems to work.


-Tim


use strict;
use HTML::Form;
my $html = join('',<DATA>);

my $form = HTML::Form->parse($html,'localhost');
print 'Before turning red off',"\n";
print $form->dump,"\n";

my @in = $form->inputs;
for my $input (@in) {
    if($input->type eq 'checkbox') {
        for my $value ($input->possible_values) {
            next unless $value;
            if($value eq 'red') { # this is the one we want to
change
                $input->value(undef);
            }
        }
    }
}

print 'After turning red off',"\n";
print $form->dump,"\n";

__DATA__
<html>
<form action="foo" method="post">
<input type="text" name="who"><br>
<input type="checkbox" name="color" value="blue"> Blue
<br>
<input type="checkbox" name="color" value="red" checked> Red
<br>
<input type="checkbox" name="color" value="green"> Green
<br>
</form>

</html>

Reply via email to