当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Loading Html Datatable through Perl

问题描述:

I was wondering if there is a way that my index.html can call a perl handler to load a datatable within the index.html. No JQuery, but I can use Javascript. How would /should I go about this? As of now I'm just printing some html in the perl handler, but I really don't like doing that if I don't have to. So basically, is there a way I can get data from a perl cgi handler and take that data and load it into a html datatable. Thanks for tips / advice.

What I'm doing now is just calling the handler using :

 <form action="http://myExampleHandler.pl.cgi" method="get">

<input type="submit" value="GO" />

</form>

Perl:

 $dtd =

"-//W3C//DTD XHTML 1.0 Transitional//EN\"

\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";

print "Content-type: text/html\n\n";

print( start_html( { dtd => $dtd,

title => "Database mySQL example" } ) );

print "Ok let\'s start...<p>";

$dbh = DBI->connect('dbi:mysql:MyDbName','MyUserName','MyPassword')

or die "Connection Error: $DBI::errstr\n";

$sql = "select * from Animal";

$sth = $dbh->prepare($sql);

$sth->execute

or die "SQL Error: $DBI::errstr\n";

#@data = $sth->fetchrow_array();

#print header;

#print "Content-Type: text/html\n\n";

print "<table width=\"900\"> \n";

print "<tr><td></td><td>Name</td><td>Type</td><td></td></tr>\n";

my($animalId, $animalName, $animalType);

while (@row = $sth->fetchrow_array) {

$animalId = $row[0];

$animalName = $row[1];

$animalType = $row[3];

print "<tr><td></td><td>$animalName</td><td>$animalType</td><td></td></tr>\n";

}

print "</table>\n";

print( end_html() );

I guess my preferred way would be to a javascript call this handler and have the handler pass back all the rows in a array or json. Then it's easy, I would just have to load the datatable with the data.

网友答案:

Of course. jQuery is written in javascript, so anything done by jQuery can be done in javascript. You need to send an XMLHttpRequest (or as it's more popularly known: an AJAX request) to your perl program. Your perl program can then send back any string as a response. Your js code can read that string, and then insert it (or anything else) into your html table--all without reloading a new page.

分享给朋友:
您可能感兴趣的文章:
随机阅读: