Latest web development tutorials

AJAX 데이터베이스

AJAX 데이터베이스와 쌍방향 통신을 위해 사용될 수있다.


AJAX 데이터베이스 인스턴스

AJAX를 통해 웹 페이지가 데이터베이스에서 정보를 읽어 방법 다음 예제는 설명합니다 :


Customer info will be listed here...


예로 설명 - HTML 페이지

위의 드롭 다운 목록에서 사용자가 고객을 선택하면, "showCustomer ()"함수라는 이름의 실행. 은 "onchange를"이벤트로이 기능은 트리거 :

<!DOCTYPE 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>

<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>

자료 설명 :

당신이 고객 (str.length == 0)을 선택하지 않는 경우, 함수는 txtHint 자리를 지 웁니다 다음 기능을 종료합니다.

당신이 고객을 선택하면, showCustomer () 함수는 다음과 같이 수행

  • XMLHttpRequest 객체를 생성
  • 서버가 응답을 수행 할 준비가되었을 때 기능 만들기
  • 요청을 전송하는 서버에 파일
  • URL 매개 변수 (Q)의 끝에 추가 참고하시기 바랍니다 (드롭 다운 목록의 내용을 포함)

ASP 파일

이 페이지를 호출하는 자바 스크립트를 통해 상기 서버는 "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>")
%>