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

Is there a common way or best practice, for creating a HTML "checkbox" form field and marking it checked?

This is what I started using:
print qq{ <input type="checkbox" name="fieldName" value="1" @{[ ($fiel +dValues{fieldName}) ? 'checked="checked"' : '' ]} /> };

Replies are listed 'Best First'.
Re: Marking "checkbox" html field checked
by LanX (Saint) on Sep 17, 2024 at 23:30 UTC
    This print line approach to HTML is ugly and others will probably come up with better templating solutions. (Please consider at least multiline here-docs)

    Anyway, if you wanna stick with variable interpolation, using a dedicated hash might be better (shorter, self documenting, less error prone,...)

    my %CHECKED = ( "" => "", 1 => 'checked="checked"'); print "<yadda $CHECKED{ !! $fieldValues{fieldName} } yadda>"

    If you don't like the !! double negation make either sure that the values are only true or false (resp. binary) or use a tied hash to do the boolean mapping inside the FETCH.

    Making %CHECKED constant or immutable might be a good idea too.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

Re: Marking "checkbox" html field checked
by Danny (Chaplain) on Sep 18, 2024 at 18:53 UTC
    I prefer to keep such things as simple as possible unless there is a significant performance advantage. I'd prefer something like:
    my $checked = $fieldValues{fieldName} ? 'checked="checked"' : ''; print qq{<input type="checkbox" name="fieldName" value="1" $checked/>\ +n};
    or possibly
    printf qq{<input type="checkbox" name="fieldName" value="1" %s/>\n}, $ +fieldValues{fieldName} ? 'checked="checked"' : '';
Re: Marking "checkbox" html field checked
by NERDVANA (Priest) on Sep 19, 2024 at 04:11 UTC
    The problem doesn't really come up anymore, for me, because all my apps are rendering checkboxes using a front-end javascript library of some sort :-)

    My perl controllers just deliver data as JSON, and the JS renders the form. Back when I still rendered checkboxes with Perl I'd be using Template toolkit, or Text::Xslate

Re: Marking "checkbox" html field checked
by stevieb (Canon) on Sep 19, 2024 at 04:44 UTC
    Is there a common way or best practice, for creating a HTML "checkbox" form field

    Yes. Use a templating system.

    and marking it checked?

    Yes. Use the templating system to pass the variable along, or use JavaScript/jQuery.

    With that said, please, for the love of all things good, make sure this isn't an attempt to sucker someone into something that they then have to opt-out of. Any field that subscribes or commits someone to something should ALWAYS be unchecked by default. In such situations, default enabled is unscrupulous at best, and outright immoral and unethical (and more) at worst.

      should ALWAYS be unchecked by default

      ok... but we have no idea what this checkbox is for. Maybe it's to opt-in? Maybe it's to opt-out? Maybe it's to select which flavors of gummie the user wants?

Re: Marking "checkbox" html field checked
by ikegami (Patriarch) on Sep 17, 2024 at 20:26 UTC

    Can you wrap your code with <c></c> (instead of <pre></pre>) so we can read it

      I changed it.
Re: Marking "checkbox" html field checked
by Anonymous Monk on Sep 18, 2024 at 15:14 UTC
      Looks like "they" deleted it

       Sorry, this post has been removed by the moderators of r/perl.

      Which is strange, because Reddit is tolerant to double cross posting.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        Doesn't look like a double post. Did you mean cross posting? And if so, why would they care? Surely their mods can't monitor the entire internet for cross-postings.

        Oh, old.reddit.com just says [removed], but new.reddit.com says Sorry, this post has been removed by the moderators of r/perl.