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

Checkbox is like the simplest HTML element there is, yet defaulting the checkbox is vexing. I've researched this and can't find or determine a definitive answer. Some sites say I need to use:
-checked => "on"
to check a box, others say
-checked => 1
and still others mandate
-checked => "checked"
What I really find though with Perl CGI is
-checked => anything
checks the box. Even if anything=undef ??! What you'd EXPECT is that -checked => 0, or "off", or "", or undef, or SOMETHING would toggle it off. No other HTML control field I know if is this ambigious or vexing. The only way I can find to toggle checked-by-default is to have two seperate statements with logic , one WITH -checked and one without, which is more 2X the syntax I want to use. Anyhow- perhaps a complete definitive explaination of defaulting checkboxes here will eventually get googled and help some other unfortunate JAPH who spent way too long on this seemingly trivial issue.

Replies are listed 'Best First'.
Re: What's the REAL DEAL with Perl $cgi->checkbox()
by MidLifeXis (Monsignor) on Feb 03, 2015 at 19:01 UTC

    Historically, the CHECKED keyword in HTML does not have a value -- it is simply a flag to the checkbox to indicate that it is turned on. If the flag is there, it is on, if not, it is off. The value (if any) is irrelevant. This is an HTML thing, not related to Perl itself.

    --MidLifeXis

Re: What's the REAL DEAL with Perl $cgi->checkbox()
by kennethk (Abbot) on Feb 03, 2015 at 19:16 UTC
    So, from the docs:
    2. The optional second parameter (-checked) specifies that the checkbox is turned on by default. Synonyms are -selected and -on.
    So, if you omit -checked, the box is unchecked. I'd say this is fairly straightforward, though I would have implemented it with a boolean evaluation myself.
    The only way I can find to toggle checked-by-default is to have two seperate statements with logic , one WITH -checked and one without, which is more 2X the syntax I want to use.
    Assuming you want a branch, there must be a test in any case. To handle the branching, you could use the ternary Conditional Operator with a list argument:
    print checkbox(-name=>'checkbox_name', $checked ? (-checked=>1) : (), -value=>'ON', -label=>'CLICK ME');
    And, to be fair, weird accumulated crufty behavior is exactly why CGI.pm has been removed from the Perl core.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      You probably know this but I want to further the discussion for hysterical historical consideration. FWIW, I don’t find this weird or crufty; it’s perfectly logical and consistent with HTML. checked is either there or not in an input[type=checkbox]. It’s not a key with a value, it’s just there. Giving it a value of undef or 0 is meaningless because it takes no value and you’re still giving it the “key” attribute. CGI being removed has more to do with there being better, c.f. more maintainable alternatives and not wanting to be seen as encouraging 1990s’ web dev practices out of the box. There is no package in any language that does what it does better or with more DWIWness. It’s just not a good way to do big code projects. If you know it well though it’s still an excellent, rapid choice for one-offs or tests. Wrapping in PSGI is easy now too.

      (To pedantic anonymonks itching to shoot: I know checked="checked".)

        I quite agree with all the above. By "weird accumulated crufty behavior" I didn't intend to say all this behavior was necessarily the Perl community's fault. The Web is a wildly different place than when people crafted in raw HTML, and modern frameworks are built around making GUI's, not publishing documents. The base technology hasn't changed, but the expectations have.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.