Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

why in perl array value is not listing on dropdown list using html template?

my perl cgi page dropdown menu doesn;t listing the values . my cgi script

use HTML::Template;use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $template = HTML::Template->new(filename => 'test.tmpl');
@count = ("abc,xyz,123,test");
print $cgi->start_form(
                        -name => 'mainpage',
                        -method => 'POST',
                        );


$template->param( COUNT => @count);
print $template->output();

my test.tmpl

<div class="row ">
            <div class="col-lg-8 col-lg-offset-2">
                                <div class="dropdown">
                                <div class="input-field col s6">
                                <input name="count" id="countid" type="text" class="validate">
                                <label for="count">count</label>
    <select>
        <TMPL_LOOP NAME="COUNT">
        <option value="<TMPL_VAR NAME=VALUE>"><TMPL_VAR NAME=NAME></option>
        </TMPL_LOOP>
    </select>                                
</div>

in the drop down menu, I am expecting below values. Please suggest if I need to make any change on above code on cgi or html template
abc
xyz
123
test

Please find the modified the cgi script values. cgi page doesn;t show the values in the dropdown tab. however in the html page rendered these values

$count = [
{ name => 'count1', value => 1 },
{ name => 'count2', value => 2 },
{ name => 'count3', value => 3 },
];

   $template->param(COUNT => [{name => 'count1', value => 1}, {name =>  'count2', value => 2}, {name => 'count3', value => 3}]);

print $template->output, "
";

html page rendered below

<div class="row ">
            <div class="col-lg-8 col-lg-offset-2">
                <div class="dropdown">
            <div class="input-field col s6">
                <input name="count" id="countstate" type="text" class="validate">
                            <label for="count">count</label>
<select>

<option value="1">count1</option>

<option value="2">count2</option>

<option value="3">count3</option>

</select>                                

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your array contains a single element: the string abc,xyz,123,test. If you want each of those things to be separate elements, you need to create your array differently:

@count = ('abc', 'xyz', 123, 'test');
@count = qw(abc xyz 123 test);

Update: OK, after reading the docs for <TMPL_LOOP>, it looks like you actually want an arrayref of hashrefs:

$count = [
    { name => '???', value => 'abc' },
    { name => '???', value => 'xyz' },
    { name => '???', value => '123' },
    { name => '???', value => 'test' },
];

$template->param(COUNT => $count);
$template->param(COUNT => [{...}, {...}, {...}, ...]);  # same thing

However, I can't tell from your question which one should be the label and which one should be the value, so I'll leave that as an exercise for you to complete.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...