下面的例子将演示网页如何通过 AJAX 从数据库读取信息: 请在下面的下拉列表中选择一个客户:
<html><!DOCTYPE html> |
<html> |
<head> |
<script> |
function showCustomer(str) |
{ |
var xmlhttp; |
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","/try/ajax/getcustomer.php?q="+str,true); |
xmlhttp.send(); |
} |
</script> |
</head> |
<body> |
<form action=""> |
<select name="customers" onchange="showCustomer(this.value)" style="font-family:Verdana, Arial, Helvetica, sans-serif;"> |
<option value="APPLE">Apple Computer, Inc.</option> |
<option value="BAIDU ">BAIDU, Inc</option> |
<option value="Canon">Canon USA, Inc.</option> |
<option value="Google">Google, Inc.</option> |
<option value="Nokia">Nokia Corporation</option> |
<option value="SONY">Sony Corporation of America</option> |
</select> |
</form> |
<br> |
<div id="txtHint">Customer info will be listed here...</div> |
</body> |
</html> |
当用户在上面的下拉列表中选择某个客户时,会执行名为 "showCustomer()" 的函数。该函数由 "onchange" 事件触发:
function showCustomer(str) |
{ |
var xmlhttp; |
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.html?q="+str,true); |
xmlhttp.send(); |
} |
showCustomer() 函数执行以下任务:
由上面的 JavaScript 调用的服务器页面是 PHP 文件,名为 "getcustomer.php"。
用 PHP 编写服务器文件也很容易,或者用其他服务器语言。请看用 PHP 编写的相应的例子。
"getcustomer.php" 中的源代码负责对数据库进行查询,然后用 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>") |
%> |