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.
| [reply] [d/l] [select] |
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"' : '';
| [reply] [d/l] [select] |
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 | [reply] |
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.
| [reply] |
|
| [reply] |
Re: Marking "checkbox" html field checked
by ikegami (Patriarch) on Sep 17, 2024 at 20:26 UTC
|
| [reply] [d/l] [select] |
|
| [reply] |
Re: Marking "checkbox" html field checked
by Anonymous Monk on Sep 18, 2024 at 15:14 UTC
|
| [reply] |
|
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.
| [reply] [d/l] |
|
| [reply] |
|
|
Oh, old.reddit.com just says [removed], but new.reddit.com says Sorry, this post has been removed by the moderators of r/perl.
| [reply] [d/l] [select] |