国产gaysexchina男同gay,japanrcep老熟妇乱子伦视频,吃奶呻吟打开双腿做受动态图,成人色网站,国产av一区二区三区最新精品

AJAX 數據庫

2018-07-15 11:29 更新

AJAX Database 實例


AJAX 可用來與數據庫進行動態(tài)通信。


AJAX 數據庫實例

下面的例子將演示網頁如何通過 AJAX 從數據庫讀取信息: 請在下面的下拉列表中選擇一個客戶:

Example

Select a customer: Alfreds Futterkiste North/South Wolski Zajazd
Customer info will be listed here...

嘗試一下 ?


實例解釋 - showCustomer() 函數

當用戶在上面的下拉列表中選擇某個客戶時,會執(zhí)行名為 "showCustomer()" 的函數。該函數由 "onchange" 事件觸發(fā):

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() 函數執(zhí)行以下任務:

  • 檢查是否已選擇某個客戶
  • 創(chuàng)建 XMLHttpRequest 對象
  • 當服務器響應就緒時執(zhí)行所創(chuàng)建的函數
  • 把請求發(fā)送到服務器上的文件
  • 請注意我們向 URL 添加了一個參數 q (帶有輸入域中的內容)

AJAX 服務器頁面

由上面的 JavaScript 調用的服務器頁面是 PHP 文件,名為 "getcustomer.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("")
do until rs.EOF
  for each x in rs.Fields
    response.write("")
    response.write("")
  next
  rs.MoveNext
loop
response.write("
" & x.name & "" & x.value & "
")
%>
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號