{"id":148,"date":"2004-02-11T21:53:36","date_gmt":"2004-02-12T01:53:36","guid":{"rendered":"http:\/\/scruss.com\/blog\/?p=148"},"modified":"2004-02-11T21:53:36","modified_gmt":"2004-02-12T01:53:36","slug":"scripting-radio-buttons","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2004\/02\/11\/scripting-radio-buttons\/","title":{"rendered":"Scripting Radio Buttons"},"content":{"rendered":"<p>I use Perl&#8217;s <a href=\"http:\/\/search.cpan.org\/search?query=HTML%3A%3ATemplate&#038;mode=all\">HTML::Template <\/a> module a lot. It allows you to write web pages that are dynamically modified by the controlling Perl CGI\/mod_perl application.<\/p>\n<p>Most of my applications fill in forms from values in a database. This is easy enough when you are filling text fields, but if you ever use radio buttons, things kind of fall down.<\/p>\n<p>I&#8217;ve found a way around this. Let&#8217;s say you have a status field that can have three values:<\/p>\n<ul>\n<li>active<\/li>\n<li>blocked<\/li>\n<li>retired<\/li>\n<\/ul>\n<p>So in Perl I define three constants:<\/p>\n<pre>\n  use constant STATUS_ACTIVE =&gt; 'active';\n  use constant STATUS_BLOCK =&gt; 'block';\n  use constant STATUS_RETIRE =&gt; 'retire';\n<\/pre>\n<p>Then in the template, I have something like this:<\/p>\n<pre>\n  &lt;input type=\"radio\" name=\"status\"\n   &lt;!-- TMPL_IF NAME=STATUS_ACTIVE --&gt;\n   checked=\"checked\"\n   &lt;!-- \/TMPL_IF --&gt;\n  value=\"active\" \/&gt;Active\n\n  &lt;input type=\"radio\" name=\"status\"\n   &lt;!-- TMPL_IF NAME=STATUS_BLOCK --&gt;\n   checked=\"checked\"\n   &lt;!-- \/TMPL_IF --&gt;\n\n  value=\"block\" \/&gt;Blocked\n\n  &lt;input type=\"radio\" name=\"status\"\n   &lt;!-- TMPL_IF NAME=STATUS_RETIRE --&gt;\n   checked=\"checked\"\n   &lt;!-- \/TMPL_IF --&gt;\n  value=\"retire\" \/&gt;Retired\n<\/pre>\n<p>If the status variable is $account-&gt;status, say, I&#8217;d use:<\/p>\n<pre>\n  $template-&gt;param(\n   STATUS_ACTIVE =&gt; ($account-&gt;status eq STATUS_ACTIVE),\n   STATUS_BLOCK =&gt;  ($account-&gt;status eq STATUS_BLOCK),\n   STATUS_RETIRE =&gt; ($account-&gt;status eq STATUS_RETIRE)\n  );\n<\/pre>\n<p>and, magically, the template picks up the right value.<\/p>\n<p>If the status variable isn&#8217;t set to one of the three predefined values, you get a radio group that none of the values is selected. You might wish to think about how you&#8217;d deal with that, perhaps setting a safe default.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I use Perl&#8217;s HTML::Template module a lot. It allows you to write web pages that are dynamically modified by the controlling Perl CGI\/mod_perl application. Most of my applications fill in forms from values in a database. This is easy enough when you are filling text fields, but if you ever use radio buttons, things kind [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[1708,187],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-general","tag-html","tag-perl"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-2o","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":0,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}