我很努力地指出這是失敗的地方。我可以讓這個插件在我的本地PC上使用php/jquery工作,但是試圖在我的生產環境中使用Perl複製它(因爲我不會考慮使用php,所以我不會參考)。來自Perl的JQuery自動完成功能
index.cgi - 這是生成包含文本框的html頁面的Perl代碼。
#!/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 - 這是在後臺的JSON饋送到自動填充運行的代碼:
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use DBI;
use JSON;
my $cgi = CGI->new;
print $cgi->header(-type => "application/json", -charset => "utf-8");
my $dbh = DBI->connect('dbi:mysql:hostname=test;database=test',"test","test");
my $term = $cgi->param('term');
my $sth = $dbh->prepare(qq{SELECT customer.name, customer.id FROM test WHERE customer.name ?;}) or die $dbh->errstr;
$sth->execute($term.'%') or die $sth->errstr;
my $json = {};
while(my @customer = $sth->fetchrow_array()) {
$json->{$customer[0]} = $customer[1];
}
print JSON::to_json($json);
test.js - 這是實際的JQuery正在使用:
$(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 was " + this.value);
}
});
});
我一直在谷歌周圍,但沒有找到任何堅實的地方有人有一個與JQuery Perl的例子。 test.js和index.cgi文件幾乎完全複製jquery自動完成的jquery-ui示例文件中的代碼,除了index.cgi是使用CGI.pm在Perl中編寫的。
任何幫助將不勝感激,再次由於服務器的性質和其上的應用程序,我在這裏的語言有所限制。
test.pl甚至不進行編譯。 – tadmc 2011-04-30 12:50:57
我知道你說過你不會進入它,但我真的很好奇你爲什麼不能在你的服務器上安裝PHP – 2011-04-30 13:02:15
'use strict'和'warnings'缺失。不要浪費時間忽略這些。 – Lumi 2011-04-30 15:26:13