AJAX 資料庫
AJAX 可用於與資料庫進行互動式通訊。
AJAX 資料庫實例
下面的實例將示範網頁如何透過AJAX 從資料庫讀取資訊:
實例
##Customer info will be listed here...
#實例解釋- HTML 頁面當當使用者在上面的下拉清單中選擇某位客戶時,會執行名為"showCustomer()" 的函數。函數由"onchange" 事件觸發:
<!DOCTYPE html>
############################### #<form> ###<select name="customers" onchange="showCustomer(this.value)">###<option value="">Select a customer:</option># ##<option value="ALFKI">Alfreds Futterkiste</option>###<option value="NORTS ">North/South</option>###<option value="WOLZA"> ;Wolski Zajazd</option>###</select>###</form>###<br>###<div id="txtHint">Customer info will be listed here. ..</div>######</body>###</html>#######<html>
<head>
<script>
function showCustomer (str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp. status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcustomer.asp ?q="+str,true);
xmlhttp.send();
}
</script>
</head
<body>
<head>
<script>
function showCustomer (str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp. status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcustomer.asp ?q="+str,true);
xmlhttp.send();
}
</script>
</head
<body>
原始碼解釋:
如果沒有選擇客戶(str.length==0),那麼函數會清空 txtHint 佔位符,然後退出函數。
如果已選取一位客戶,則showCustomer() 函數會執行下列步驟:
建立XMLHttpRequest 物件
建立在伺服器回應就緒時執行的函數
向伺服器上的檔案傳送請求
請注意新增至URL 末端的參數(q )(包含下拉清單的內容)
ASP 檔案
#上面這段透過JavaScript 呼叫的伺服器頁面是名為"getcustomer.asp" 的ASP文件。
"getcustomer.asp" 中的原始程式碼會執行一次針對資料庫的查詢,然後在HTML 表格中傳回結果:
<%
response.expires= -1
sql="SELECT * FROM CUSTOMERS WHERE CUSTOMERID="
sql=sql & "'" & request.querystring("q") & "'"
#set conn=Server. CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs =Server.CreateObject("ADODB.recordset")
rs.Open sql,conn
response.write("<table>")
do until rs.EOF
for each x in rs.Fields
response.write("<tr><td><b>" & x.name & "</b></td>")
response.write ("<td>" & x.value & "</td></tr>")
next
rs.MoveNext
loop
response.write("</ table>")
%>
response.expires= -1
sql="SELECT * FROM CUSTOMERS WHERE CUSTOMERID="
sql=sql & "'" & request.querystring("q") & "'"
#set conn=Server. CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs =Server.CreateObject("ADODB.recordset")
rs.Open sql,conn
response.write("<table>")
do until rs.EOF
for each x in rs.Fields
response.write("<tr><td><b>" & x.name & "</b></td>")
response.write ("<td>" & x.value & "</td></tr>")
next
rs.MoveNext
loop
response.write("</ table>")
%>