Thread: [htmltmpl] Radio button question
Brought to you by:
samtregar
From: C H. <hag...@ep...> - 2004-02-11 15:33:36
|
I'm a newbie to HTML::Template, and this is my first post to the list .... My question has to do with radio buttons, and any recommended ways of carrying a checked value through successive forms. I have my forms set up so submission errors (a missing required field, for example) are "carried through" ... that is, the form is regenerated, and any fields filled out in the first submission retain their respective values at the next iteration. This all works fine, and I've been able to figure out how to carry drop-down lists values along as well. Example: Choose an item from the list: <select name="Item"> <!--TMPL_IF NAME="Item" --> <option selected><!--TMPL_VAR NAME=Item --> <!--/TMPL_IF--> <option> <option value="1">One <option value="2">Two <option value="3">Three </select> What I'm trying to do is figure out a way to do something similar with radio buttons .... in other words, carry a "checked" value through to the next form generation. The wrinkle that's causing me to scratch my head is that the field-name value is the same for each radio button, so I *can't* do something like: <input type="radio" name="Color" value="Red" <!--TMPL_IF NAME="Color" --> checked <!--/TMPL_IF--> "> <input type="radio" name="Color" value="Blue" <!--TMPL_IF NAME="Color" --> checked <!--/TMPL_IF--> "> I'm wondering if I'm missing something simple here ..... I've got things working with a pretty cludgy looking work around: In the programming: ##CH likely a better way to do this if ($q->param('Color') eq 'Red') { $q->param('Red','checked') }; if ($q->param('Color') eq 'Blue') { $q->param('Blue','checked') }; And in the template: <input type="radio" name="Color" value="Red" <!--TMPL_VAR NAME=Red -->"> <input type="radio" name="Color" value="Blue" <!--TMPL_VAR NAME=Blue -->"> But the above is really ugly, and I gotta believe there's a better approach (that I'm too dense to recognize) Any suggestions? Carl hag...@ep... |
From: Timm M. <tm...@ag...> - 2004-02-11 15:42:27
|
At 10:32 AM 2/11/04 -0500, C Hagstrom wrote: <> >And in the template: > ><input type="radio" name="Color" value="Red" <!--TMPL_VAR NAME=Red -->"> ><input type="radio" name="Color" value="Blue" <!--TMPL_VAR NAME=Blue -->"> > >But the above is really ugly, and I gotta believe >there's a better approach (that I'm too dense to recognize) Yeah, I know, it seems like there should be a better solution. In fact, due to the limitations of HTML, there are only a series of equally sucky solutions. What you have above is as good as any. Blame W3C. |
From: Jason P. <ja...@jo...> - 2004-02-11 16:07:10
|
>> <input type="radio" name="Color" value="Red" <!--TMPL_VAR NAME=Red -->"> >> <input type="radio" name="Color" value="Blue" <!--TMPL_VAR NAME=Blue >> -->"> >> >> But the above is really ugly, and I gotta believe >> there's a better approach (that I'm too dense to recognize) > Yeah, I know, it seems like there should be a better solution. In fact, > due to the limitations of HTML, there are only a series of equally sucky > solutions. What you have above is as good as any. Blame W3C. I would recommend checking out HTML::FillInForm for your situation - it'll take care of those radio buttons and select boxes, since you're rehashing the same form if you didn't receive valid input. Something like this un-tested code: # You're processing the input from the form... if ( bad_input( $query ) ) { $template = HTML::Template->new( 'filename' => 'previous_steps_template.TMPL', 'die_on_bad_params' => 0, ); $fif = HTML::FillInForm->new; print $fif->fill( 'scalarref' => $template->output, 'fobject' => $query ); exit; } You'll be amazed how cool FillInForm will work ... you won't need to do that TMPL_VAR stuff inside the form fields ... FillInForm will handle all that for you. HTH, Jason |
From: Bill M. <mo...@ha...> - 2004-02-11 16:02:09
|
On Wed, Feb 11, 2004 at 10:32:49AM -0500, C Hagstrom wrote: > My question has to do with radio buttons, > and any recommended ways of carrying > a checked value through successive forms. Are you talking about "sticky" forms? > I have my forms set up so submission errors > (a missing required field, for example) are > "carried through" ... that is, the form is regenerated, > and any fields filled out in the first submission > retain their respective values at the next iteration. Yep, that's sticky forms. Use HTML::FillInForm and you don't have to bother with all of that. -- Bill Moseley mo...@ha... |
From: Keith J. <kja...@ey...> - 2004-02-11 16:03:10
|
The easiest way (and I'm sure others will recommend this too) is to use HTML::FillInForm. The next best way (IMHO) is to do a simple variation of your second method: perl: $tmpl->param( $q->param('Color')."_Checked" => "checked"); template: <input type="radio" name="Color" value="Red" <!--TMPL_VAR NAME=Red_Checked -->"> <input type="radio" name="Color" value="Blue" <!--TMPL_VAR NAME=Blue_Checked -->"> On Wed, 2004-02-11 at 10:32, C Hagstrom wrote: > I'm a newbie to HTML::Template, and this > is my first post to the list .... > > My question has to do with radio buttons, > and any recommended ways of carrying > a checked value through successive forms. > > I have my forms set up so submission errors > (a missing required field, for example) are > "carried through" ... that is, the form is regenerated, > and any fields filled out in the first submission > retain their respective values at the next iteration. > > This all works fine, and I've been able to figure out > how to carry drop-down lists values along as well. Example: > > Choose an item from the list: > <select name="Item"> > <!--TMPL_IF NAME="Item" --> > <option selected><!--TMPL_VAR NAME=Item --> > <!--/TMPL_IF--> > <option> > <option value="1">One > <option value="2">Two > <option value="3">Three > </select> > > What I'm trying to do is figure out a way to do something > similar with radio buttons .... in other words, carry a "checked" > value through to the next form generation. The wrinkle > that's causing me to scratch my head is that the field-name > value is the same for each radio button, so I *can't* do something > like: > > <input type="radio" name="Color" value="Red" > <!--TMPL_IF NAME="Color" --> > checked > <!--/TMPL_IF--> > "> > > <input type="radio" name="Color" value="Blue" > <!--TMPL_IF NAME="Color" --> > checked > <!--/TMPL_IF--> > "> > > I'm wondering if I'm missing something simple here ..... > I've got things working with a pretty cludgy looking work around: > In the programming: > > ##CH likely a better way to do this > if ($q->param('Color') eq 'Red') { > $q->param('Red','checked') > }; > if ($q->param('Color') eq 'Blue') { > $q->param('Blue','checked') > }; > > And in the template: > > <input type="radio" name="Color" value="Red" <!--TMPL_VAR NAME=Red -->"> > <input type="radio" name="Color" value="Blue" <!--TMPL_VAR NAME=Blue -->"> > > But the above is really ugly, and I gotta believe > there's a better approach (that I'm too dense to recognize) > > Any suggestions? > > Carl > hag...@ep... > > > > > > > > ------------------------------------------------------- > SF.Net is sponsored by: Speed Start Your Linux Apps Now. > Build and deploy apps & Web services for Linux with > a free DVD software kit from IBM. Click Now! > http://ads.osdn.com/?ad_id=1356&alloc_id=3438&op=click > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users -- Keith Jackson <kja...@ey...> |
From: Cees H. <ce...@si...> - 2004-02-11 16:03:22
|
C Hagstrom wrote: > I'm wondering if I'm missing something simple here ..... > I've got things working with a pretty cludgy looking work around: > In the programming: > > ##CH likely a better way to do this > if ($q->param('Color') eq 'Red') { > $q->param('Red','checked') > }; > if ($q->param('Color') eq 'Blue') { > $q->param('Blue','checked') > }; > > And in the template: > > <input type="radio" name="Color" value="Red" <!--TMPL_VAR NAME=Red -->"> > <input type="radio" name="Color" value="Blue" <!--TMPL_VAR NAME=Blue -->"> > > But the above is really ugly, and I gotta believe > there's a better approach (that I'm too dense to recognize) That is exactly how I used to handle that situation as well. Until I started using HTML::FillInForm! Using HTML::Template to fill in form elements is a pain, and it clutters up the templates... HTML::FillInForm will do all the hard work for you, without needing to worry if it's a checkbox, radio button, or just a plain input box you are dealing with. Just render your template, then run it through HTML::FillInForm before you send it out to the client. The only caveat is that it needs to parse the entire HTML file to find the form elements and alter them, so it decreases the perfomance of your app slightly (not enough to worry me though). Cheers, Cees |