我想将数据库中的值从一个选择框(即ostype)填充到另一个选择框(host_name)。host_name 中的值应从数据库中填充。我已经为此编写了 ajax 代码,这样我就不必刷新来填充值。我知道这段代码不完整,但我被困在这一点上。如何才能做到这一点 ?
这是cgi代码:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use FindBin qw($Bin);
use logic;
my @host;
my $i=0;
my $q = CGI->new();
my $ostype=$q->param("ostype");
my $sth=logic->fetchos($ostype);
while(my @row = $sth->fetchrow()){
$host[$i]=$row[0];
$i++;
}
my $JSCRIPT=<<EOF;
function call()
{
var x = document.getElementbyID("ostype").value;
if (x)
$.ajax({
type:"GET",
url:"cgi-bin/logic.cgi",
contentType: "application/json; charset=utf-8",
dataType: "json",
data:"ostype="+x,
});
}
}
EOF
;
print $q->header,
$q->start_html
(
-title=>'Record Inserted',
),
$q->center($q->h1('Requested Input')), $q->br,"\n",
$q->start_form
(
-method=>'post',
-action=>'inputcgi.cgi',
), "\n",
"Operating system",
$q->scrolling_list(-name=>'ostype',
-size=>3,
-id=>'ostype',
-values=>['RHEL','Windows','Linux'],
-defaults=>['Select OS'],
-multiple=>'true',
-onClick=>"call()"),$q->br,"\n",
"Host name",
$q->scrolling_list(-name=>'host_name',
-size=>3,
-values=>[@host],
-defaults=>['Select servers'],
-multiple=>'true'),$q->br,"\n",
$q->submit(-value=>'Submit');
$q->end_form,
$q->end_html;
逻辑 perl 模块
sub new{
$class=shift;
$self={
_host_name=>'',
};
bless $self,$class;
return $self;
}
sub fetchos{
my($self,$host_name)=@_;
my $sth=$dbh->prepare("select DISTINCT host_name from HOST_LIST where os='$os' ");
$sth->execute() or die "$DBI::errstr";
return $sth;
}
逻辑.cgi
#!usr/bin/perl
use CGI;
use logic;
use CGI::Carp qw(fatalsToBrowser);
use FindBin qw($Bin);
print"hello";
perl 模块正在工作,因为我填充了选择框中的所有值,但它不适用于 ostype 的特定值。