手册
目录
下面的例子演示:网页如何通过 AJAX 从数据库中读取信息:
点击 "运行实例" 按钮查看在线实例
当用户在上面的下拉列表中选择一位客户后,执行名为 "showCustomer()" 函数。此函数被 onchange 事件触发:
function showCustomer(str) {
var xhttp;
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xhttp.open("GET", "getcustomer.asp?q=" + str, true);
xhttp.send();
}
showCustomer() 函数进行如下:
被以上 JavaScript 调用的服务器页面是名为 "getcustomer.asp" 的 ASP 文件。
使用 PHP 或其他服务器语言能够轻松重写该服务器文件。
请参见对应的 PHP 实例。
"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("customers.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
rs.Open sql,conn
response.write("| " & x.name & " | ") response.write("" & x.value & " |
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.2万人学习
共49课时
77万人学习
共29课时
61.7万人学习
共25课时
39.3万人学习
共43课时
70.9万人学习
共25课时
61.6万人学习
共22课时
23万人学习
共28课时
33.9万人学习
共89课时
125万人学习