in reply to Creating HTML radio button group with text field

Other then omitting the <br> tag in between the last radio box and the text box, this should do the trick get you started. Personally, i would use HTML::Template instead. This is some hairy code :P
use strict; use CGI qw(:standard); use CGI::Pretty; my @question = ('a'..'d'); my $answer = [ [('number',5,7,13)], [('color','red','green','fuschia')], ]; print header, start_html, start_form; foreach my $i (0..$#$answer) { # remove type of question from front and add 'Other' to end my $thingy = shift @{$answer->[$i]}; push @{$answer->[$i]}, 'Other (please specify)'; print "What is your favorite $thingy?", br, radio_group( -name => sprintf('%s%02d','question',$i + 1), -labels => { map { $question[$_] => $answer->[$i]->[$_] } (0..$#question)}, -values => \@question, -linebreak => 'true', ), textfield(sprintf('%s%02d','other',$i + 1)), p; } print submit, end_form, end_html;

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Creating HTML radio button group with text field
by impossiblerobot (Deacon) on May 05, 2002 at 03:52 UTC
    Thanks, jeffa, but this doesn't quite meet my requirements. I'm sorry if my question wasn't clear. As I said in my original post (but did not include in my HTML sample):
    I need to be able to optionally add (for example) a text field to any of the radio options.
    Here's another example to clarify:
    Where did you hear about this product? <input type="radio" name="question" value="a">Friend</input><br> <input type="radio" name="question" value="b">Magazine (please specify +:)</input> <input type="text" name="magname"><br> <input type="radio" name="question" value="c">Brochure</input><br> <input type="radio" name="question" value="d">Other (please specify:)< +/input> <input type="text" name="other"><br>
    I hope this makes it clearer what I want to be able to do.

    Impossible Robot
      Sorry impossiblerobot - i indeed did miss that rather clear requirement. How about a cleaner HTML::Template solution?
      use strict; use CGI qw(header); use HTML::Template; my $data = do {local $/; <DATA>}; my $template = HTML::Template->new( scalarref => \$data, ); $template->param( questions => [ { question => 'What is your favorite number?', radio => [ { regular => 'question01', value => 'a', label => 5, }, { regular => 'question01', value => 'b', label => 7, }, { regular => 'question01', value => 'c', label => 13, }, { special => 'question01', value => 'd', label => 'Other', text => 'other', }, ], }, { question => 'Where did you hear about this product?', radio => [ { regular => 'question02', value => 'a', label => 'Friend', }, { special => 'question02', value => 'b', label => 'Magazine', text => 'magname', }, { regular => 'question02', value => 'c', label => 'Brochure', }, { special => 'question02', value => 'd', label => 'Other', text => 'other', }, ], }, ], ); print header, $template->output; __DATA__ <form> <tmpl_loop questions> <p> <tmpl_var question><br/> <tmpl_loop radio> <tmpl_unless special> <input type="radio" name="<tmpl_var regular>" value="<tmpl_va +r value>"/><tmpl_var label><br/> <tmpl_else> <input type="radio" name="<tmpl_var special>" value="<tmpl_va +r value>"/><tmpl_var label> (please specify:) <input type="text" name="<tmpl_var text>"/><br/> </tmpl_unless> </tmpl_loop> </p> </tmpl_loop> <input type="submit"> </form>
      Now for the fun part - getting the right values out! You might want to prefix each text box with the letter and call them all 'other'. For example, instead of calling the Magazine textbox 'magname', call it something along the lines of 'b-other' so you can dynamical retrieve it from CGI::param(). This is just off the top of my head (/me winks at BUU), but you get the point. Good luck!

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        Thanks, jeffa. I'm using TT2, but I could easily adapt your second solution, if it turned out to be the best solution in this case. (I normally don't like to put that much logic/code in my templates, but that doesn't mean it might not be useful in some instances.)

        Impossible Robot