Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

HTML as radio_group label w/

by Anonymous Monk
on Sep 25, 2005 at 03:58 UTC ( #494852=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This seems like a relatively obvious thing, but since I've seen no sign of an answer in the official docs on CPAN and Google is getting me nowhere, I've come here in the hopes that someone will know off the top of their head.

I'm using to build a web page that will have a group of moderately complex radio buttons. I say "moderately complex" only because the labels for these buttons will be some string followed by a drop-down box (i.e. something done with a select tag). Now, in theory, I would have assumed that the following would work:
my %labels; my $labels{'foo'} = "String" . popup_menu(-name=>'bar', -values=>\@foobard); print radio_group(-name=>'clearchannel', -values=>'foo', -labels=>\@labels); ...
The problem is, the label is being surrounded by an HTML <label> tag, which means that the HTML contained there doesn't get rendered; in fact, instead of my select box, I get the HTML that would be used to create it.

Clearly I can work around this by just printing my radio_group by hand, but that's ugly and a pain to maintain. I had thought that -nolabels=>1 would solve my problem, but it just suppresses my label text altogether. Does anyone know how to make these stupid <label> tags go away, so that my select boxes will actully display that way?

Alex Kirk

Replies are listed 'Best First'.
Re: HTML as radio_group label w/
by davis (Vicar) on Sep 25, 2005 at 11:35 UTC

    Your code could definitely benefit from use strict; and use warnings; — you're declaring a hash (and also creating it at the same time), and then you try and treat it like an array when actually expects a hash reference (you can do magic to make something like this work, but you don't want to).

    Something like this does what you mean:

    use warnings; use strict; use CGI qw(:standard); use Data::Dumper; my %labels = ( monkey => "sigmund", cat => "felix", dog => "fido", ); print radio_group(-name=>'clearchannel', -values=>[keys %labels], -labels=>\%labels);
    Here I'm creating a hash (the my %labels lines). Pass the list of possible values for the group (monkey; cat; dog) as an array reference created by the [] operator (keys merely returns the keys of a hash as a list). Then provide the labels for each of the values by passing a hashref.

    Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
      Actually, I am using strict in my code (though not warnings) -- I just used a small slice of a larger program, since I didn't want to inundate everyone with a bunch of superfluous code.

      While what you've suggested above is very handy -- it does a nice job of cleaning up both my code and memory -- it totally misses the point of what I was asking. The heart of my problem is that, when I have HTML tags in a radio button label, they're spit out inside of <label> tags such that web browsers see them as literal text, not actual HTML, and they don't get properly rendered. For example, instead of getting the string "Year:" followed by a select box on a new line, I'm getting:
      Year:<br><select name="year_year" tabindex="1"> <option selected="sele +cted" value="2005">2005</option> <option value="2006">2006</option> < +option value="2007">2007</option> <option value="2008">2008</option> +<option value="2009">2009</option> <option value="2010">2010</option> + </select>
      Which is distinctly not helpful. Looking at the page source, I'm seeing:
      <label><input type="radio" name="date_selection" value="year" tabindex +="7" />Year:&lt;br&gt;&lt;select name=&quot;year_year&quot; tabindex= +&quot;1&quot;&gt;&#10;&lt;option selected=&quot;selected&quot; value= +&quot;2005&quot;&gt;2005&lt;/option&gt;&#10;&lt;option value=&quot;20 +06&quot;&gt;2006&lt;/option&gt;&#10;&lt;option value=&quot;2007&quot; +&gt;2007&lt;/option&gt;&#10;&lt;option value=&quot;2008&quot;&gt;2008 +&lt;/option&gt;&#10;&lt;option value=&quot;2009&quot;&gt;2009&lt;/opt +ion&gt;&#10;&lt;option value=&quot;2010&quot;&gt;2010&lt;/option&gt;& +#10;&lt;/select&gt;</label>
      That said, does anyone know how to make the HTML in my label actually render as such, and not output directly to the screen?

      Alex Kirk
Re: HTML as radio_group label w/
by randyk (Parson) on Sep 25, 2005 at 18:17 UTC
    You could set autoEscape to be undefined, as in the following example:
    use warnings; use strict; use CGI qw(:standard); autoEscape(undef); my %provinces = (ab => 'Alberta', on => 'Ontario', ); my %states = (vt => 'Vermont', ca => 'California', ); my %countries = (ca => 'Canada ' . popup_menu(-name => 'Province', -values => [keys %provinces], -labels => \%provinces, ), us => 'United States ' . popup_menu(-name => 'State', -values => [keys %states], -labels => \%states, ), ); print header, start_html, start_form, radio_group(-name => 'country', -values => [keys %countries], -labels => \%countries, -columns => 2, ), submit, end_form, end_html;
      Aha! That's exactly what I was looking for. Thanks a ton! :-)
Re: HTML as radio_group label w/
by Tanktalus (Canon) on Sep 25, 2005 at 16:11 UTC

    And this is why most experienced perl users seem to suggest using a templating system instead of CGI for creating HTML output. There are many religious wars on this site on which template system to use, so I won't go into that, but just leave you with the suggestion to use a template module instead of CGI for this.

      Great, a holy war...the exact opposite of productivity. I guess I'll do this the annoying way (i.e. just manually spit out my HTML), since I'm not about to go and redo an entire site using templating.

      Tanktalus, thanks for warning me -- I hate pointless flame wars, and you've probably helped me avoid one. That said, if anyone knows how to do what I'm trying to the way, I'd appreciate hearing about it.

      Alex Kirk
        Speaking of holy wars, I hear radio buttons are much harder to type in vi

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://494852]
Approved by sgifford
[Discipulus]: preoccupied.. ;=)
Discipulus stopped with turkisk spices anyway.. but better hashes than arrays!
[oiskuu]: Discipulus, from the edge or from the top?
[Discipulus]: oiskuu from the top aka 0-0 coords
[LanX]: edge or node?
[oiskuu]: from each height difference, there's a fixed number of "moves right" - "moves left". So it's a number of combinations.
[Discipulus]: I already know how much distinct paths exists but to demonstrate the property i want to have them all
[oiskuu]: ie C(n, k); where n is height difference, k is position in the line
[Discipulus]: infact there are 70 distinct path to reach the tale wich holds 70

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2018-03-19 11:01 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (239 votes). Check out past polls.