AJAX 数据库实例

AJAX 可用来与数据库进行相互的通信。

AJAX 数据库实例

下面的例子演示网页如何通过 AJAX 从数据库中读取信息:

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function showCustomer(str)
  5. {
  6. var xmlhttp;
  7. if (str=="")
  8. {
  9. document.getElementById("txtHint").innerHTML="";
  10. return;
  11. }
  12. if (window.XMLHttpRequest)
  13. {// code for IE7+, Firefox, Chrome, Opera, Safari
  14. xmlhttp=new XMLHttpRequest();
  15. }
  16. else
  17. {// code for IE6, IE5
  18. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  19. }
  20. xmlhttp.onreadystatechange=function()
  21. {
  22. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  23. {
  24. document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  25. }
  26. }
  27. xmlhttp.open("GET","/ajax/getcustomer.asp?q="+str,true);
  28. xmlhttp.send();
  29. }
  30. </script>
  31. </head>
  32. <body>
  33. <form action="" style="margin-top:15px;">
  34. <label>请选择一位客户:
  35. <select name="customers" onchange="showCustomer(this.value)" style="font-family:Verdana, Arial, Helvetica, sans-serif;">
  36. <option value="APPLE">Apple Computer, Inc.</option>
  37. <option value="BAIDU ">BAIDU, Inc</option>
  38. <option value="Canon">Canon USA, Inc.</option>
  39. <option value="Google">Google, Inc.</option>
  40. <option value="Nokia">Nokia Corporation</option>
  41. <option value="SONY">Sony Corporation of America</option>
  42. </select>
  43. </label>
  44. </form>
  45. <br />
  46. <div id="txtHint">客户信息将在此处列出 ...</div>
  47. </body>
  48. </html>

实例解释 - HTML 页面

当用户在上面的下拉列表中选择某位客户时,会执行名为 "showCustomer()" 的函数。该函数由 "onchange" 事件触发:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. function showCustomer(str)
  6. {
  7. if (str=="")
  8. {
  9. document.getElementById("txtHint").innerHTML="";
  10. return;
  11. }
  12. if (window.XMLHttpRequest)
  13. {// 针对 IE7+, Firefox, Chrome, Opera, Safari 的代码
  14. xmlhttp=new XMLHttpRequest();
  15. }
  16. else
  17. {// 针对 IE6, IE5 的代码
  18. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  19. }
  20. xmlhttp.onreadystatechange=function()
  21. {
  22. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  23. {
  24. document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  25. }
  26. }
  27. xmlhttp.open("GET","getcustomer.asp?q="+str,true);
  28. xmlhttp.send();
  29. }
  30. </script>
  31. </head
  32. <body>
  33.  
  34. <form>
  35. <select name="customers" onchange="showCustomer(this.value)">
  36. <option value="">Select a customer:</option>
  37. <option value="ALFKI">Alfreds Futterkiste</option>
  38. <option value="NORTS ">North/South</option>
  39. <option value="WOLZA">Wolski Zajazd</option>
  40. </select>
  41. </form>
  42. <br>
  43. <div id="txtHint">客户信息将在此处列出...</div>
  44.  
  45. </body>
  46. </html>

源代码解释:

如果没有选择客户(str.length 等于 0),那么该函数会清空 txtHint 占位符,然后退出该函数。

如果已选择一位客户,则 showCustomer() 函数会执行以下步骤:

  • 创建 XMLHttpRequest 对象
  • 创建在服务器响应就绪时执行的函数
  • 向服务器上的文件发送请求
  • 请注意添加到 URL 末端的参数(q)(包含下拉列表的内容)

ASP 文件

上面这段 JavaScript 调用的服务器页面是名为 "getcustomer.asp" 的 ASP 文件。

"getcustomer.asp" 中的源代码会运行一次针对数据库的查询,然后在 HTML 表格中返回结果:

  1. <%
  2. response.expires=-1
  3. sql="SELECT * FROM CUSTOMERS WHERE CUSTOMERID="
  4. sql=sql & "'" & request.querystring("q") & "'"
  5.  
  6. set conn=Server.CreateObject("ADODB.Connection")
  7. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  8. conn.Open(Server.Mappath("/db/northwind.mdb"))
  9. set rs=Server.CreateObject("ADODB.recordset")
  10. rs.Open sql,conn
  11.  
  12. response.write("<table>")
  13. do until rs.EOF
  14. for each x in rs.Fields
  15. response.write("<tr><td><b>" & x.name & "</b></td>")
  16. response.write("<td>" & x.value & "</td></tr>")
  17. next
  18. rs.MoveNext
  19. loop
  20. response.write("</table>")
  21. %>