i'm struggling pin point failing. can plugin work using php/jquery on local pc, trying duplicate perl on production box (where php not option reasons i'll not go into).
index.cgi - perl code generates html page contains text box.
#!/usr/bin/perl -w use dbi; use cgi; use warnings; use strict; $cgi = new cgi; $cgi->autoescape(undef); print $cgi->header; print $cgi->start_html(-title=>'test', -dtd=>'//w3c//dtd html 4.01 transitional//en', -style=>'/themes/ui-lightness/jquery.ui.all.css', -script=>[ {-type=>'javascript', -src=>'/js/jquery-1.5.2.min.js'}, {-type=>'javascript', -src=>'/js/test.js'}, {-type=>'javascript', -src=>'/ui/jquery-ui-1.8.11.custom.js'}, {-type=>'javascript', -src=>'/ui/jquery.ui.core.js'}, {-type=>'javascript', -src=>'/ui/jquery.ui.widget.js'}, {-type=>'javascript', -src=>'/ui/jquery.ui.position.js'}, {-type=>'javascript', -src=>'/ui/jquery.ui.autocomplete.js'} ] ); print $cgi->start_div({-class=>'ui-widget'}); print $cgi->textfield(-id=>'customer',-size=>25),$cgi->br; print $cgi->end_div(),$cgi->br; print $cgi->div({-class=>'ui-widget-content',-id=>'log'}); print $cgi->end_html;
test.pl - code runs in background feed json autocomplete:
#!/usr/bin/perl use warnings; use strict; use cgi; use dbi; use json; $cgi = cgi->new; print $cgi->header(-type => "application/json", -charset => "utf-8"); $dbh = dbi->connect('dbi:mysql:hostname=test;database=test',"test","test"); $term = $cgi->param('term'); $sth = $dbh->prepare(qq{select customer.name, customer.id test customer.name ?;}) or die $dbh->errstr; $sth->execute($term.'%') or die $sth->errstr; $json = {}; while(my @customer = $sth->fetchrow_array()) { $json->{$customer[0]} = $customer[1]; } print json::to_json($json);
test.js - actual jquery being used:
$(function() { function log( message ) { $( "<div/>" ).text( message ).prependto( "#log" ); $( "#log" ).attr( "scrolltop", 0 ); } $( "#customer" ).autocomplete({ source: "test.pl?term=", minlength: 2, select: function( event, ui ) { log( ui.item ? "value: " + ui.item.value + " key " + ui.item.id : "nothing selected, input " + this.value ); } }); });
i've been around google, haven't found solid has example of perl jquery. test.js , index.cgi files duplicate code jquery-ui example files jquery autocomplete except index.cgi written in perl using cgi.pm.
any appreciated, again limited in languages here due nature of server , applications on it.
you not have value or label field assigned in returned json string. dumping name , id. autocomplete not know 1 want appear in autocomplete results. assign customer name value field.
$json->{"value"} = $customer[0]; $json->{"id"} = $customer[1];
the jquery autocomplete needs "value" or "label" field returned json result. if not include it, jquery autocomplete not work:
the basic functionality of autocomplete works results of query assigned ‘label’ , ‘value’ fields. explanation on ‘label’ , ‘value’ fields jquery ui site:
“the local data can simple array of strings, or contains objects each item in array, either label or value property or both. label property displayed in suggestion menu. value inserted input element after user selected menu. if 1 property specified, used both, eg. if provide value-properties, value used label.”
link full example: http://www.jensbits.com/2011/05/09/jquery-ui-autocomplete-widget-with-perl-and-mysql/
Comments
Post a Comment