ajax与数据库交互

来源:互联网 发布:mac如何把桌面的图标 编辑:程序博客网 时间:2024/04/30 15:59
参考:http://www.w3school.com.cn/ajax/index.asp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>ajax与数据库交互</title></head><script type="text/javascript">function showCustomer(str){var xmlhttp;if(str==''){document.getElementById('txtHint').innerHTML='';return;}if(window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}else{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><body><form action="" style="margin-top:15px;">     <label>请选择一位客户:        <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>    </label></form><!--AJAX 服务器页面由上面的 JavaScript 调用的服务器页面是 ASP 文件,名为 "getcustomer.asp"。用 PHP 编写服务器文件也很容易,或者用其他服务器语言。"getcustomer.asp" 中的源代码负责对数据库进行查询,然后用 HTML 表格返回结果:<%response.expires=-1sql="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,connresponse.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.MoveNextloopresponse.write("</table>")%>--></body></html>

0 0
原创粉丝点击