Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Search</title> <script type="text/javascript" src="jquery-latest.js"></script> <script type="text/javascript"> var debug = false; $(document).ready(function() { $( "#search" ).autocomplete({ source: function(request, response) { $.ajax({ url: "search.pl", dataType: "json", data: { search: request.term, }, success: function(data) { // return data stuff here }, error: function(response){ if (debug == true) {console.log(response)}; alert('Somethings broken in the AJAX return'); } }); }, minLength: 5, delay: 500 }); }); </script> </head> <body> <div id="container"> <h2>TEST</h2> <div id="namesearch"> <form id="form_search" method="get" name="form_search"/> <label>Search: </label> <input type="text" name="search" id="search" class="search_box"/> </form> </div> <div> <p id="submitted"></p> <!-- display resutls from search here --> <div id='RESULTS'></div> <!-- end displaying results --> <!-- All Data - Here is where I want the data to display--> <TMPL_LOOP NAME=ALL_DATA> <TMPL_VAR NAME=NAME> <TMPL_VAR NAME=CITY> <TMPL_VAR NAME=STATE> </TMPL_LOOP> <TMPL_VAR NAME=COUNT> <!-- Data Ends --> </div> </body> </html>
... my $main_tmpl = HTML::Template->new(filename => 'search.tmpl', die_on_ +bad_params => 1); ... sub data { my $id = shift || return; my $sql = qq{SELECT distinct name, city, state FROM my_table WHERE name like ? order by name asc}; my $sth = $dbh->prepare($sql); $sth->execute(${id}.'%') or die "SQL Error: $DBI::errstr\n"; my $all_data = $sth->fetchall_arrayref({}); my @loop_data = (); # initialize an array to hold loop # Count things my $c = 0; my @count; foreach my $row (@{$all_data}) { $c++; my %row_data; # get a fresh hash for the row data # fill in these rows $row_data{NAME} = $row->{NAME} || ''; $row_data{CITY} = $row->{CITY} || ''; $row_data{STATE} = $row->{STATE} || ''; push @count, $c; # the crucial step - push a reference to this row into the loop push(@loop_data, \%row_data); } # Count things my $counter = @count; # I need to load the data here but it doesn't. # load values into the template $main_tmpl->param( ALL_DATA => \@loop_data, COUNT => $counter, ); # If sending data through json use this # This works if I use jQuery to process the returned json from here # but I am trying to load the data using HTML::Template =code print $q->header(-type => "application/json"); if(@loop_data){ print to_json(\@loop_data); exit; }else{ my $json_string = [{ result => "No data"}]; print to_json($json_string); exit; } =cut } # end sub
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: HTML::Template data loading
by Anonymous Monk on Sep 16, 2014 at 22:36 UTC |