ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: AJAX :: AJAX Êý¾Ý¿â

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

AJAX
AJAX ʵÀý
AJAXä¯ÀÀÆ÷
AJAXÔ´´úÂë
AJAX·þÎñ¶Ë
AJAX Êý¾Ý¿â
AJAXÓëXMLÎļþ
AJAX XMLHttpRequ..
΢ÈíµÄAjax
AJAX ½éÉÜ
AJAX HTTP ÇëÇó
AJAX ·þÎñÆ÷¶Ë½Å±..
AJAX Suggest °¸À..
AJAX ResponseXML
AJAX AppML

AJAX Êý¾Ý¿â


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-05-27 ä¯ÀÀ:3634 :: ::

AJAX can be used for interactive communication with a database.
AJAX¿ÉÒÔÓÃÀ´ºÍÊý¾Ý¶Ë½øÐÐÊý¾ÝµÄ½»»¥ÁªÍ¨¡£


AJAX Database Example
AJAX Êý¾Ý¿âʵÀý

In the AJAX example below we will demonstrate how a web page can fetch information from a database using AJAX technology.
ÔÚÒÔϵÄAJAX·¶ÀýÖУ¬ÎÒÃÇ¿ÉÒÔÁ˽⵽һ¸öÍøÒ³ÊÇÈçºÎÓÃAJAX¼¼Êõ´ÓÊý¾Ý¶Ë»ñµÃÐÅÏ¢µÄ¡£


Select a Name in the Box Below
ÇëÔÚÏÂÃæµÄ²Ëµ¥ÖÐÑ¡ÔñÒ»Ãû×Ö

Select a Customer:
Customer info will be listed here.
¿Í»§ÐÅÏ¢½«±»ÂÞÁÐÔÚÕâ¡£

AJAX Example Explained
AJAX ʵÀý½âÎö

The example above contains a simple HTML form and a link to a JavaScript:
ÉÏÃæµÄÀý×Ó°üº¬ÁËÒ»¸ö¼òµ¥µÄHTML±íµ¥ºÍÒ»¹ØÁªµ½JSµÄlink:

<html>
<head>
<script src="selectcustomer.js"></script>
</head>
<body>
<form> 
Select a Customer:
<select name="customers" onchange="showCustomer(this.value)">
<option value="ALFKI">Alfreds Futterkiste
<option value="NORTS ">North/South

<option value="WOLZA">Wolski Zajazd
</select>
</form>
<p>
<div id="txtHint"><b>Customer info will be listed here.</b></div>
</p>
</body>
</html>

As you can see it is just a simple HTML form with a drop down box called "customers".
ÕýÈçÄãËù¼ûÕâÖ»ÊÇÒ»¸ö¼òµ¥µÄHTML±íµ¥£¬ÀïÃæÓÐÒ»¼òµ¥µÄÏÂÀ­²Ëµ¥£¬ÆäÃû³ÆÎª"customers"

The paragraph below the form contains a div called "txtHint". The div is used as a placeholder for info retrieved from the web server.
±íµ¥ÏÂÃæ°üº¬Ò»¸öÃûΪ"txtHint"µÄDIV£¬¸ÃDIV±»ÓÃÀ´×öΪ·´À¡´ÓWEB·þÎñÆ÷¼ìË÷ÐÅÏ¢µÄռλ·û

When the user selects data, a function called "showCustomer()" is executed. The execution of the function is triggered by the "onchange" event. In other words: Each time the user change the value in the drop down box, the function showCustomer is called.
µ±Óû§Ñ¡ÔñÊý¾Ý£¬Ò»¸ö³ÆÎª "showCustomer()"µÄº¯Êý±»Ö´ÐÐÁË¡£Õâ¸öº¯ÊýÓÉ"onchange"ʼþËù´¥·¢¡£¿ÉÒÔÕâô½²£ºÃ¿µ±Óû§¸Ä±äÏÂÀ­²Ëµ«ÖеÄÃû×Ö£¬º¯Êý¾Í»áÖ´ÐÐ

The JavaScript code is listed below.
JSµÄ´úÂëÔÚÏÂÃæ


The AJAX JavaScript
AJAXµÄJS

This is the JavaScript code stored in the file "selectcustomer.js":
ÕâÊÇJS´úÂ룬±»±£´æÔÚÒ»½Ð×ö"selectcustomers.js"µÄÎļþÄÚ£º

var xmlHttp

function showCustomer(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getcustomer.asp"

url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

 


The AJAX Server Page
AJAX·þÎñÒ³

The server paged called by the JavaScript, is a simple ASP file called "getcustomer.asp".
·þÎñÒ³ÓÉJSËùµ÷Dz£¬ÊÇÒ»³ÆÎª"gecustomer.asp"µÄ¼òµ¥ASPÎļþ

The page is written in VBScript for an Internet Information Server (IIS). It could easily be rewritten in PHP, or some other server language.
¸ÃҳʹÓõÄÊÇÕë¶ÔIISµÄVBSÓïÑÔ£¬µ±È»Ò²¿ÉÒÔ¸Äд³ÉÏñPHP»òÊÇÆäËûһЩ·þÎñÓïÑÔ

The code runs an SQL against a database and returns the result as an HTML table:
´úÂëÔËÐÐÁËSQLÀ´´ÓÊý¾Ý¿âÖн«½á¹û·µ»Øµ½HTML±í¸ñÖУº

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

ÆÀÂÛ (3) 1 All