-1

I want to populate the values in database from one select box i.e. ostype to other select box(host_name). The values in host_name should get populated from the database. I have written the ajax code for that so that i don't have to refresh to populate values. i know this code is not complete but i am stuck at this point of code. How can this be done ?

here is the cgi code:

#!/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;

logic perl module

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;
}

logic.cgi

#!usr/bin/perl
use CGI;
use logic;
use CGI::Carp qw(fatalsToBrowser);
use FindBin qw($Bin);

print"hello";

the perl module is working as i populated all the values in the selectbox but it is not working for specific values of ostype.

himani
  • 9
  • 1

1 Answers1

0

This is still not a CGI script - it is missing the vitally important HTTP header section. Run your script on the command line or point your browser at it and compare the results with the other script to see the difference.

#!usr/bin/perl
use CGI;
use logic;
use CGI::Carp qw(fatalsToBrowser);
use FindBin qw($Bin);

print"hello";

Just the simple addition of a print $q->header(); is needed, like you had in your original script. Since your AJAX is expecting the result to be JSON not HTML, you'll also need to specify the format of the result.

#!usr/bin/perl
use CGI;
use logic;
use CGI::Carp qw(fatalsToBrowser);
use FindBin qw($Bin);

my $q = CGI->new();
print $q->header('application/json');
print "[\"hello\"]";

I've also taken the liberty of changing your content into something that will be valid JSON

Chris Turner
  • 8,082
  • 1
  • 14
  • 18