Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Think about Loose Coupling
 
PerlMonks  

HTML::Template with checkbox

by bebewinla (Novice)
on Sep 04, 2012 at 23:14 UTC ( #991701=perlquestion: print w/ replies, xml ) Need Help??
bebewinla has asked for the wisdom of the Perl Monks concerning the following question:

I use HTML::Template to display a list of files with the checkbox next to it. Here is a snip of the html template.

<TMPL_LOOP NAME="files_loop"> <tr id="TMPL_VAR ID>"> <td><TMPL_VAR NAME ="index"></td> <td><TMPL_VAR NAME ="filename"> </td> <td><input type="checkbox" name="field" <TMPL_VAR NAME="check">> </td> </tr> </TMPL_LOOP> <form action="index.cgi" method="post"> <input type="submit" name="Edown" value=" Download "> <input type="hidden" name="cmd" value="download"> </form>

In my cgi script, I was able to display the output I wanted using print $template->output. Now I am unable to obtain the checkbox values along with the respective filenames when user check the boxes and select download button. I'd like to get a list of filenames that were checked. I tried q->param("field") does not seem working at all. Please help

Comment on HTML::Template with checkbox
Download Code
Re: HTML::Template with checkbox
by choroba (Abbot) on Sep 04, 2012 at 23:48 UTC
    Your problem is not related to Perl. You should move your checkboxes into the form.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: HTML::Template with checkbox
by GrandFather (Cardinal) on Sep 05, 2012 at 02:38 UTC

    Show us runnable sample code that demonstrates the problem. Note that template loops with nested data require a nested data structure so your sample code should show us how you create that structure. Most likely you want something like:

    use strict; use warnings; use HTML::Template; my $html = <<HTML; <TMPL_LOOP NAME="files_loop"> <tr id="TMPL_VAR ID>"> <td><TMPL_VAR NAME ="index"></td> <td><TMPL_VAR NAME ="filename"> </td> <td><input type="checkbox" name="field" <TMPL_VAR NAME="check">> </td> </tr> </TMPL_LOOP> <form action="index.cgi" method="post"> <input type="submit" name="Edown" value=" Download "> <input type="hidden" name="cmd" value="download"> </form> HTML my $tmpl = HTML::Template->new(scalarref => \$html); my @loopParams = ( {index => 1, filename => 'plonk', check => 'f1'}, {index => 2, filename => 'pip', check => 'f2'} ); $tmpl->param(files_loop => \@loopParams); print $tmpl->output();

    prints:

    <tr id="TMPL_VAR ID>"> <td>1</td> <td>plonk </td> <td><input type="checkbox" name="field" f1> </td> </tr> <tr id="TMPL_VAR ID>"> <td>2</td> <td>pip </td> <td><input type="checkbox" name="field" f2> </td> </tr> <form action="index.cgi" method="post"> <input type="submit" name="Edown" value=" Download "> <input type="hidden" name="cmd" value="download"> </form>
    True laziness is hard work

      I have my cgi script very similar to what you have. hastable contains the filenames to be output it. I don't have the f1,f2...values for the check, not sure if it's needed.

      $q = new CGI; $template = HTML::Template->new(filename => "$displaypage", associate => $q); my @loop; foreach $index (sort {$a <=> $b} keys %hashtable) { my ($fn) = @{$hashtable{$index}}; my %row = ( index => $index, fn => $fn , check => " "); push (@loop, \%row); } $template->param(files_loop=> \@loop); send_header(); print $template->output;

      so after user click on the checkboxes for the files that he wants to download, then he click on the "download" button, in my cgi script it will go to "sub downloadFiles", in here I am not sure how to get the values of the checkboxes or filenames , I tried this :

      sub downloadfiles { @files = $q->param("field"); ... }
      It did not get any values in @files. Not sure what values it should returned. I tried you suggestion adding f1,f2 with instead using index number 1,2,3... but still don't know how to get the checkboxes values. Please help.

        Oh, silly me. You don't provide a value attribute for your checkbox so nothing gets returned. You need something like:

        <td><input type="checkbox" name="field" value="<TMPL_VAR NAME="fi +lename">">

        for your checkbox line. That will return the file name if the checkbox is checked.

        True laziness is hard work
Re: HTML::Template with checkbox
by rpnoble419 (Pilgrim) on Sep 05, 2012 at 03:14 UTC
    I agree with choroba, your issue is not a Perl one. I would add a value to each of your check boxes and use the file name, this way when you click on a check box, the file name will be the returned value. If you use a unique name for each check box then the files will be returned as individual parameters. If you use the same name then the file names will be returned using a single parameter. Use CGI.pm to sort out the parameters. If you go the same name route, CGI.pm will return all of the filenames in a single array....
    <td><input type="checkbox" name="field" <TMPL_VAR NAME="check"> value= +"<TMPL_VAR NAME ="filename">"

      Could you show me how to get the the names of checkbox or filenames with your added value="<TMPL_VAR NAME ="filename">"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991701]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-04-20 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls