Perl Mason get value of a checkbox

1.7k views Asked by At

How do i get the value of a checkbox and put it in a variable for insertion in DB?

$request->param(cb) is not getting the value
the checkbox is in a mason2 component(.mc) and the value of the checkbox will be pass to another component. so i have to find a way to get the value when it is submitted
here is the code

<input type="checkbox" name="cb" value="" onclick="$(this).attr('value', this.checked ? 1 : 0)">  
3

There are 3 answers

3
David On BEST ANSWER

For tasks like this, the combination of WWW::Mechanize and HTML::TokeParser is your friend. The former helps you navigate by clicking buttons, links, etc. and the latter slices and dices the HTML.

In this case, you simply navigate to the page, scan the data for 'input' tags (i.e. a 'checkbox' is a type of 'input') and then test each input to see whether the 'name' attribute matches the one you're after. Once you've found the correct 'input' tag, then you just grab the value of the 'checked' attribute, which you can then use a you like.

For example:

use WWW::Mechanize;
use HTML::TokeParser;

my $checkbox_name = "cb";
my $url = 'http://some_url/';

my $page = WWW::Mechanize->new(autocheck =>1);
$page->get($url);
my $pagedata = HTML::TokeParser->new(\$page->{content});

while (my $token = $pagedata->get_tag("input")) {
    my @tokenarray = @$token;
    my $attr_ref = $tokenarray[1];
    my %attr = %$attr_ref;

    # dump the contents of the hash
    foreach my $k (keys %attr) {
        print "$k: $attr{$k}\n";
    }
    print "\n";

    if ($attr{name} eq $checkbox_name) {
        print "found it!\n";
        print "status = ", $attr{checked}, "\n";
        last;
    }
}

In this example, the "navigation" part is trivial and you could probably get by without WWW::Mechanize. However, in some cases, you need to submit input, click buttons, use links, etc. so it often comes in handy.

2
clt60 On

Simplify the code:

<input type="checkbox" name="cb" value="1">  

By definition, when the checkbox is checked, the browser will send the value, if the checkbox is not checked will send nothing. You want convert "nothing" into "0", so simply in you Mason2 component do:

has 'cb';

#... and where you need the 0/1 checkbox value simply use the $.cb
my $cb01 = $.cb ? 1 : 0;

So, when the the checkbox is clicked, will get 1, otherwise 0.

Adding a full example:

Mason2 component: cbf.mc #checkbox Form

<form action="cbs">
    <input type="checkbox" name="cb" value="1">
    <input type="submit">
</form>

Mason2 component: cbs.mc #checkbox Show

 <%class>
 has 'cb';
 </%class>

 The checkbox value is: <% $.cb ? 1 : 0 %>

Make the above two components, and point your browser into:

 http://url/path/to/cbf

You will get a simple form. Now leave unchecked or check the checkbox (as you wish) and press submit. The component cbs.mc will show the value. When checked you will get "1" when not "0".

Really simple.

Or, you try the following cbs.mc

<%class>
has 'cb' => (default => 0);
</%class>

The chebox value is: <% $.cb %>

More clear and employ default value. Both examples are working solutions.

0
Greg On

I have a similar problem and stumbled upon this thread while trying to hash it out. In my scenario, I have several checkboxes with the same value (think 'yes/no' per row in a database). I came up with what I find to be a simple and easy to understand solution.

Let's assume our values will be 'Y' (checked) or 'N' (unchecked).

Add a 'N' hiddden field for every line item. This ensures that you receive a value for every row. The checkbox field of the same name will then tell us whether things are a 'Y' or not. What you will end up with, assuming 4 elements with the second and last checked is an array of qw(N N Y N N Y). Just convert to a string, swap 'NY' values for 'Y' and convert back. You'll get the proper qw(N Y N Y).

#HTML
%foreach ... {
<input type=hidden name=shouldwe value='N'><input type=checkbox name=shouldwe value='Y'>
%}

#Processing
$list = join('', @shouldwe);
$list =~ s/NY/Y/g;
@shouldwe = split(//, $list);