Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    HTML::Templates (checkboxes)

    I cant find anything that tells me how to set up a template variable for checkboxes and/or radio buttons. Can it be done?

    Here is a text field using HTML::Templates
    value=<TMPL_VAR NAME="myfieldname">

    Is it possible to use HTML::Templates to set one or more of the following checkboxes to "checked"? I guess the same type of thing, but of course only one selected, with radio button groups?

    <input type="checkbox1" name="chk1" value="1" checked>
    <input type="checkbox2" name="chk2" value="2">
    <input type="checkbox3" name="chk3" value="3">

    Thanks,
    -tdavis

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Here's an example taken from one of my scripts.
    Code:
    <TMPL_LOOP name="CATEGORIES">
    <input type="radio" class="aligncenter" id="category" name="category" value="<tmpl_var name="VALUE">" <tmpl_var name="CHECKED"> /><tmpl_var name="VALUE">
    </TMPL_LOOP>
    Code:
    my @category = create_category_array();
    
    $template->param(CATEGORIES => \@category);
    
    sub create_category_array {
    
       my @categories = qw(OSH NETWORKING SUPPORT);
       my @category;
    
       for (@categories) {
          my %category;
          $category{'VALUE'} = $_;
          $category{'CHECKED'} = 'checked="checked"' if exists $form{'category'} && $form{'category'} eq $_;
          push @category, \%category;
       }
       return @category;
    }

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    In your HTML example, you dont have mutlitple radio buttons defined, you have a TMPL_LOOP that is loaded up in the Perl script? I have not found that in any of the HTML::Template stuff I have found. Thanks!

    What about checkboxes?
    Are they handled the same way?

    In my example, I guess I made a mistake. They are all type=checkbox, but have different names because more than one can be selected.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Quote Originally Posted by tdavis View Post
    In your HTML example, you dont have mutlitple radio buttons defined, you have a TMPL_LOOP that is loaded up in the Perl script? I have not found that in any of the HTML::Template stuff I have found. Thanks!

    What about checkboxes?
    Are they handled the same way?

    In my example, I guess I made a mistake. They are all type=checkbox, but have different names because more than one can be selected.
    TMPL_LOOP expects to be passsed (by reference) an array-of-hashes.
    Here's the module's documentation. http://search.cpan.org/~samtregar/HT...e.pm#TMPL_LOOP

    The only (key) difference between my example and the module's is the method used to build/declare the array reference.

    My example is using radio buttons, but checkboxes are handled in the same way.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    OK. So my checkbox value (example below) will not work? I have to use a TMPL_LOOP? It does not appear to work this way. If I set the value of the CHECK to "yes" I would expect it to be checked, if it is blank then not checked. But, it is checked either way when I tried this.

    <input type="checkbox" name="field" checked="<TMPL_VAR NAME="check">">

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    You need to put 'checked=' as part of the template param and offcially, the value of the checked attribute should be 'checked' not 'yes'.

    $template->param(check => 'checked="checked"');

    <input type="checkbox" name="field" <TMPL_VAR NAME="check">>

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I think this is obvious, but if you dont want anything checked, you simply don't assign a value to the param i.e. it should have a value of undef or an empty string.

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks. I got it to work using <input type="checkbox" name="field" <TMPL_VAR NAME="check">> where the value of the template variable "check" is either blank or "checked". I hope is the correct way to do it. But I am not sure what you mean by $template->param(check => 'checked="checked"'); I did like this: $template->param(checkbox => $checked);. It works anyway.

    Now onto drop down boxes. I bet they work the same way, but using "selected" as a variable name like this: <option value="Sports" selected>SPORTS Price Group</option>. Does that look right?

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Oops. Like this with the variable:

    <option value="Sports" <TMPL_VAR NAME="choice4">>SPORTS Price Group</option>

  • #10
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    The drop down box worked too!

    Thanks again FishMonger!
    You have been such a great help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •