我正在創建一個包含3個基本元素的網頁,選擇框,按鈕,&表。從高層次,用戶選擇一個元素,然後單擊該按鈕。單擊按鈕時,會執行PERL腳本,將數據放入mySQL數據庫的表中。所有這些事情都能成功。現在使用PERL和jquery-ajax檢索mysql數據
,我想數據庫表中的值與動態表回到我的HTML文件。但是我發現每個源都有我的CGI文件寫html標籤。我不覺得這是對的,因爲我不瞭解我的CGI知道表ID,儘管我正在傳遞價值。另外,我知道我的js文件是不正確的,有兩個單獨的AJAX調用,但這是我在家裏邏輯上處理我的頭。
我不知道什麼是第一個,修復CGI或JS文件。
HTML代碼(的index.html):
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js" charset="utf-8"></script>
<script type="text/javascript" class="jsbin" src="C:/xampp/DataTables/media/js/jquery.dataTables.js" charset="utf-8"></script>
<script type="text/javascript" src="js/main.js" charset="utf-8"></script>
<script type="text/javascript" src="js/RunPerlScript.js" charset="utf-8"></script>
<script type="text/javascript" src="js/table.js" charset="utf-8"></script>
</head>
<body>
<header>
<h1></h1>
</header>
<form name="myForm" method="GET" action="">
<select id="cdLDAP" >
<option/>
</select>
<input type="button" id="btn_run" name="btn_run" value="Run"></input>
</form>
<table id="results_table">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>
</body>
</html>
的Perl(CGI):
#!/usr/bin/perl -T
use CGI;
use DBI;
use strict;
use warnings;
# read the CGI params
my $cgi = CGI->new;
my $inputselection = $cgi->param("cdLDAP");
my $html_table = $cgi->param("html_table_results");
my ($base_dn, $tblname);
#my $password = $cgi->param("password");
#my $inputselection = "Disabled Users";
#LDAP Connection parameters
if ($inputselection eq "Disabled Users") {
$base_dn = "";
$tblname = "disabled_user";
} elsif ($inputselection eq "") {
$base_dn = "";
$tblname = "service_account";
} elsif ($inputselection eq "") {
$base_dn = "";
$tblname = "";
} else {
die;
}
# connect to the database
my ($platform,$database,$host,$port,$db_user,$pw) = ("mysql","results","localhost","3306","resultsuser","mysql123");
my $dsn = "DBI:$platform:database=$database,host=$host,port=$port";
my $connect = DBI->connect("DBI:mysql:database=$database;host=$host",$db_user,$pw,{RaiseError => 1});
#query db to get results set for table output
my $query_results = "SELECT * FROM " . $tblname;
my $query_handle = "";
$query_handle = $connect->prepare($query_results) or die $connect->errstr;
$query_handle->execute() or die $query_handle->errstr;
print header;
# HTML for the beginning of the table
# we are putting a border around the table for effect
print "<table border=\"1\" width=\"800\"> \n";
# print your table column headers
print "<tr><td>User ID</td><td>Status</td><td>Last Password Reset</td><td>Reset Needed?</td></tr>\n";
my (@data,$uid,$status,$pwlstset,$resetmsg);
# retrieve the values returned from executing your SQL statement
foreach (@data = $query_handle->fetchrow_array()) {
$uid = $data[0];
$status = $data[1];
$pwlstset = $data[2];
$resetmsg = $data[3];
# print your table rows
print "<tr><td>$uid</td><td>$status</td><td>$pwlstset</td><td>$resetmsg</td></tr>\n";
}
# close your table
print "</table>\n";
# exit the script
exit;
JS/JQuery的/ AJAX:
$(function() {
$('#btn_run').click(function() {
var tblname = $('#cdLDAP').val();
var html_table = $('#results_table').attr('id');
$.ajax({
type: "GET",
url: "/perl/cgitest.pl", // URL of the Perl script that queries LDPA and inputs to mySQL
data: "cdLDAP=" +tblname,
// script call was *not* successful
error: function() {
alert("ERROR!");
}, // error
// script call was successful
// data contains the JSON values returned by the Perl script
success: function(data){
alert("success!");
} // success
}); // ajax
$.ajax({
type: "GET",
url: "/perl/cgitest2.pl", // URL of the Perl script that retirves data from mySQL
data: "cdLDAP=" +tblname +",html_table_results=" +html_table,
// script call was *not* successful
error: function() {
alert("ERROR!");
}, // error
// script call was successful
// data contains the JSON values returned by the Perl script
success: function(data){
alert("success!");
} // success
}); // ajax
});
});
因此,這裏是我需要什麼一些幫助: 1)我的PERL腳本是否應該寫html標籤?如果是的話,我怎麼寫index.html
而不是一個新的html文件? 2)如果表結構由jQuery的文件創建一個良好的教學來源將EB讚賞,因爲我想學習如何釣魚,而不是給魚。
我不familar與JSON,但將這項工作還是要做,我仍然需要進行編碼呢?'my%json_hash =(); 的foreach(@data = $ query_handle-> fetchrow_array()) \t { \t%json_hash =( \t \t \t \t 「鑰匙」=> 「用戶ID」, \t \t \t \t 「VAL」=> $數據[0], \t \t \t \t 「鑰匙」=> 「狀態」, \t \t \t \t 「VAL」=> $數據[1], \t \t \t \t 「鍵」=> 「最後密碼重置」, \t \t \t \t 「VAL」=> $數據[2], \t \t \t \t 「鑰匙」=> 「復位按需」, \t \t \t \t「VAL 「=> $數據[3] \t \t \t \t) \t}' 這是否創建一個完整的散列或只是一個給定的條目,並不斷得到消滅了?此外,從'encode'文檔中,通過將其創建爲散列,我不必使用'encode'正確的假設? – cquadrini 2012-04-20 19:06:26
暫時拋開JSON,這將不能用作Perl數據結構。我不明白你爲什麼使用「key」和「val」作爲鍵名。您當然不能在單個散列中多次使用相同的密鑰。你也會在每次循環時覆蓋'%json_hash',因爲你什麼也不做(例如將一個引用附加到數組中)。當涉及到JSON時,您需要將您的Perl數據結構轉換爲JSON(或其他基於字符串的序列化),然後再通過線路發送。 – Quentin 2012-04-20 19:10:21
https://github.com/dorward/simple-ajax-demo/blob/master/webroot/demo.pl可能會有用。 – Quentin 2012-04-20 19:13:59