19-4 �ϥ� Prototype.js �Ӷi�� AJAX �����]�p

¥Ø«e¦bºô¸ô¤W³Ì±`³Q¥Î¨ìªº JavaScript framework¡A´N¬O prototype.js¡A¥i¥Ñ¤U¦Cºô§}¤U¸ü¡G
http://www.prototypejs.org
©Ò¿×ªº framework¡A¥i¥H¬Ý¦¨¬O­ì¥ý JavaScript ªºÀ³¥Îµ{¦¡¤¶­±¡]API¡A©Î¬O Appliation Program Interface¡^¡A¥i¥H®Ú°ò©ó°ò¥»ªº JavaScript ¨Ó´£¨Ñ§ó¥ý¶iªº¥\¯à¡C§Ú­Ì¥»¤p¸`©Ò¥Îªº prototype.js ª©¥»¬O 1.5.1.1¡]2007/06/19 µo¦æ¡^¡A©Ò´£¨ÑªºÂX¥R¥\¯à¦p¤U¡G ¦b«e¤@¤p¸`§Ú­Ì¤w¸g»¡©ú¤F AJAX ªº°ò¥»¨Ï¥Î¤è¦¡¤Î½d¨Ò¡A¥»¤p¸`±N»¡©ú¦p¦ó¨Ï¥Î prototype.js ¨Ó¶i¦æ AJAX ªººô­¶³]­p¡C­º¥ý¡A§Ú­Ìª½±µ¬Ý¬Ý¤@­Ó²³æªº½d¨Ò¡G

Example¡]ajax/ajaxViaPrototype01.htm¡^¡G

³o¬O¤@­Ó¬d¸ßºq¤âªººô­¶¡A¥u­n§A¦b¤å¦rÄæ¦ì¿é¤Jºq¤â¦W¦r¡A´N¥i¥H¨Ï¥Î AJAX ªº«D¦P¨B¶Ç¿é¤è¦¡¡A¥ß§Y¹ï¸ê®Æ®w ajax/test.mdb ¶i¦æ¬d¸ß¡A¨Ã±Nºq¤â¸ê°TÅã¥Ü¦b¤å¦rÄæ¦ì¤U¤è¡C¨Ò¦p¡A·í§Ú­Ì¿é¤J¡u¾HÄR§g¡v¨Ã«ö¤U¡u°e¥X¡v®É¡A´N·|¬Ý¨ì¾HÄR§gªº¸ê°T¤w¸gÅã¥Ü¦bºô­¶¤U¤è¡G

Hint
³o¬O¤@­Ó¤ñ¸û¦Ñªº¸ê®Æ®w¡A§A¤]¥i¥H¿é¤J¨ä¥Lºq¤â¬Ý¬Ý¡A¨Ò¦p©PµØ°·¡B§Å±Ò½å¡BªL¾Ð½¬¡B²ö¤å½«¡B³³´¹¼üµ¥¡C

¦Ó³o¾ã­Ó¹Lµ{¨Ã¨S¦³¸g¥Ñºô­¶ªº­«¸ü¨Ó¹F¦¨¡C¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]ajax/ajaxViaPrototype01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>

<body>
<h2 align=center>AJAX Webpage Design Using Prototype.js</h2>
<hr>

<script src="prototype.js"></script>
<script>
// Åã¥Ü¬d¸ßµ²ªG
function showQueryResult(xmlHttpObj){
	Element.update('queryResult', xmlHttpObj.responseText);
}
// °e¥X¹ï¸ê®Æ®wªº¬d¸ß
function sendQuery() {
	var url = 'queryDb01.asp';				// ¦øªA¾¹ªºµ{¦¡ºô­¶	
	var queryString = 'singerName=' + $F('singerName');	// °Ñ¼Æ¦C
	var ajax = new Ajax.Request(url, {method:'post', parameters:queryString, onComplete:showQueryResult});
}
</script>

ºq¤â¤j¦W¡G<input type="text" id="singerName" value="¾HÄR§g">
<input type="button" onClick="sendQuery()" value="°e¥X">
<div id="queryResult"></div>

</body>
</html>

°ò¥»¤W¡APrototype.js ¥u¬O±N XMLHttpRequest ¡u¥]°_¨Ó¡v¡A°ò¥»ªº¬yµ{ÁÙ¬O¨S¦³ÅÜ¡A¥H¤W¤@¸`©Ò»¡ªº¤T­Ó°ò¥»¨BÆJ¨Ó¬Ý¡A¹ïÀ³Àɮפλ¡©ú¦p¤U¡G

  1. ªñºÝªºµo°e¨ç¼Æ¡GsendQuery()
    ¦¹¨ç¼Æ©w¸q¤F»·ºÝªºµ{¦¡½Xºô­¶¡]¦¹¨Ò¬° queryDb01.asp¡^¡A¦P®É¤]¥[¤J¤F°Ñ¼Æ¦C¡]¦¹¨Ò¬° singerName=¾HÄR§g¡^¡A³Ì«á²£¥Í AJAX ª«¥ó¨Ã©w¸q¬ÛÃö°Ñ¼Æ¡A°e¨ì»·ºÝ°õ¦æ¡C¨ä¤¤¦³´XÂI­nª`·N¡G
    • ¡u$F('singerName')¡v¬O Prototype.js ©Ò´£¨ÑªºÂ²¤Æ»yªk¡A¨ä¥\¯à¥þµ¥©ó¡udocument.getElementById('singerName').value¡v¡C¦P¼Ë¦a¡A§Ú­Ì¤]¥i¥H¨Ï¥Î¡u$('singerName')¡v¨Ó¥Nªí id ¬° singerName ªºª«¥ó¡A¨ä¥\¯à¥þµ¥©ó¡udocument.getElementById('singerName')¡v¡C

      Hint
      ¦pªG§Ú­Ì¦b $() ¤§¤º¿é¤J¦h­Ó id¡A¦¹¨ç¼Æ·|¦^¶Ç¹ïÀ³³o¨Ç id ªºª«¥ó©Ò¦¨ªº°}¦C¡C

    • ²£¥Í AJAX ª«¥óªº²Ä¤G­Ó°Ñ¼Æ¬O¡u{method:'post', parameters:queryString, onComplete:showQueryResult}¡v¡A¦¹°Ñ¼Æ¥þµ¥©ó¤@­Ó¨ã¦³¤T­ÓÄæ¦ìªºª«¥ó¡A¦]¦¹§Ú­Ì¬Æ¦Ü¥i¥H±N sendQuery() ¨ç¼Æªº²Ä¤T¦C±Ô­z¡G var ajax = new Ajax.Request(url, {method:'post', parameters:queryString, onComplete:showQueryResult}); §ï¦¨¤U¦Cµ{¦¡½X¡G var ajaxParam = new Object(); ajaxParam.method='post'; ajaxParam.parameters=queryString; ajaxParam.onComplete=showQueryResult; var ajax = new Ajax.Request(url, ajaxParam); ©Ò±o¨ìªºµ²ªG¬O¤@¼Ëªº¡C
  2. »·ºÝªºµ{¦¡½X¡GqueryDb01.asp
    ¦¹µ{¦¡½X¦p¤U¡G

    ­ì©lÀÉ¡]ajax/queryDb01.asp¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
    <%@language=jscript%>
    <%//AJAX remot program in charge of database query%>
    <%Response.Charset="utf-8"%>
    <%
    // ¶Ç¦^¬d¸ß¸ê®Æ®wªº²Ä¤@µ§¸ê®Æ
    function getFirstRecordFromQueryResult(database, sql){
    	var Conn = Server.CreateObject("ADODB.Connection");
    	Conn.ConnectionString = "DBQ=" + Server.MapPath(database) + ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;";
    	Conn.Open();
    	var RS = Conn.Execute(sql);
    	var out="<p><b>Query Result of <u>"+sql+"</u>:</b><p>";
    	if (RS.EOF)
    		return(out+"No data found!");
    	for (i=0; i<RS.Fields.Count; i++)
    		out=out+"<font color=red>"+RS(i).Name+"</font>: <font color=blue>"+RS(i)+"</font><br>";
    	RS.Close();
    	Conn.Close();
    	return(out);
    }
    
    database="test.mdb";		// ¸ê®Æ®w¦WºÙ
    sql="SELECT * from Singer where ChineseName = '"+Request("singerName")+"'";	// ³y¥X SQL «ü¥O
    outStr=getFirstRecordFromQueryResult(database, sql);				// ¦^¶Ç²Ä¤@µ§¬d¸ßµ²ªG
    Response.write(outStr);		// ¦L¥X¬d¸ßµ²ªG
    %>

    ¨ä¥\¯à«Ü²³æ¡A§Y¬O±µ¦¬¨Ï¥ÎªÌ¿é¤Jªººq¤â¦W¦r¡A³y¥X SQL ©R¥O¡A¨Ã°e¤J¸ê®Æ®w¬d¸ß¡A¨Ã±N¬d¸ßµ²ªG¦L¥X¨Ó¡C

  3. ªñºÝªº±µ¦¬¨ç¼Æ¡GshowQueryResult()
    ¦b¦¹¨ç¼Æ¤¤¡A§Ú­Ì¥u¨Ï¥Î¤@¦Cµ{¦¡½X¡G Element.update('queryResult', xmlHttpObj.responseText); ³o¤]¬O Prototype.js ©Ò´£¨ÑªºÂ²¤Æ»yªk¡A¨ä¥\¯à¥þµ¥©ó document.getElementById('queryResult').innerHTML=xmlHttpObj.responseText
¥t¥~ÁÙ¦³¤@ÂI­n¯S§Oª`·N¡G¥Ñ©ó¸ê®Æ®w©Mºô­¶½s½Xªº¤£¤@­P¡A±`·|¾É­P¶Ã½Xªº²£¥Í¡A¦]¦¹§Ú­Ì³o­Ó½d¨Òªº¨â­ÓÀɮס]ajaxViaPrototype01.htm ©MqueryDb01.asp¡^¡A³£¬O±Ä¥Î UTF-8 ªº½s½X¡A¦]¦¹ÅªªÌ¦b¶}±Ò³o¨ÇÀɮ׮ɡA­nª`·N§Aªº¤å¦r½s¿è¾¹¬O§_¦³¤ä´© UTF-8 ½s½XªºÀÉ®×½s¿è¥\¯à¡C

Hint
¤@¯ë±`¥Îªº¤å¦r½s¿è¾¹¡A¨Ò¦p°O¨Æ¥»¡]notepad.exe¡^¤Î UltraEdit¡A³£¤ä´© UTF-8 ¤å¦rÀɪºÀ˵ø©M½s¿è¡C

¨Æ¹ê¤W¡A¤W­zªº½d¨ÒÁÙ¥i¥H§ó²³æ¡A¦pªG§Ú­Ì¥u¬O±N¬d¸ßµ²ªGÅã¥Ü¦bºô­¶¤W¡A¥i¥H§ï¥Î¥t¤@­Ó Prototype.js ¹ï©ó AJAX ©Ò´£¨Ñªº¨ç¼Æ Ajax.Updater()¡A¨Ï¥Î½d¨Ò¦p¤U¡G

Example¡]ajax/ajaxViaPrototype02.htm¡^¡G

¦¹½d¨Ò©M«e¤@­Ó½d¨Òªº¥\¯à§¹¥þ¬Û¦P¡A¬Æ¦Ü³s»·ºÝªºµ{¦¡½X³£¬O©ñ¦b queryDb01.asp¡A°ß¤@¤£¦Pªº¬O¡A§Ú­Ì§ï¥Î¤F Ajax.Updater()¡A©Ò¥H¥i¥Hª½±µ«ü©w¦^¶Ç¸ê®Æ©Ò¥²¶·§e²{ªº¦ì¸m¡A½d¨Ò­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]ajax/ajaxViaPrototype02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head>

<body>
<h2 align=center>AJAX Webpage Design Using Updater() in Prototype.js</h2>
<hr>

<script src="prototype.js"></script>
<script>
// °e¥X¹ï¸ê®Æ®wªº¬d¸ß
function sendQuery() {
	var url = 'queryDb01.asp';				// ¦øªA¾¹ªºµ{¦¡ºô­¶	
	var queryString = 'singerName=' + $F('singerName');	// °Ñ¼Æ¦C
	var ajax = new Ajax.Updater('queryResult', url, {method:'post', parameters:queryString});
}
</script>

ºq¤â¤j¦W¡G<input type="text" id="singerName" value="¾HÄR§g">
<input type="button" onClick="sendQuery()" value="°e¥X">
<div id="queryResult"></div>

</body>
</html>

¦b¤W­z­ì©l½X¤¤¡AAjax.Updater() ªº²Ä¤@­Ó°Ñ¼Æ¬O 'queryResult'¡A³o´N¬O¥Nªí¦^¶Ç¸ê®Æ±N·|³Q«ü©w¨ì document.getElementById('queryResult').innerHTML¡A¦]¦¹§Ú­Ì´N¤£¥²¥t¥~¦b¼g¤@­Ó­t³dÅã¥Üµ²ªGªº¨ç¼Æ¡A´«¥y¸Ü»¡¡A©M«e¤@­Ó½d¨Ò¤ñ¸û¡A§Ú­Ì¤w¸g¤£»Ý­n showQueryResult() ³o­Ó¨ç¼Æ¤F¡A©Ò¥H¾ã­Óºô­¶­ì©l½X¬Ý°_¨Ó§ó¥[²¼ä¡C

­Y­n¹ï AJAX ¬yµ{¶i¦æ¶i¤@¨Bªº±±¨î¡A§Ú­Ì¥i¥H¨Ï¥Î Ajax.Responders.register ¨Óµù¥U´X­Ó¨ç¼Æ¡A¥H«KÅã¥Ü AJAX ªº¤u§@¬yµ{¡C¨Ò¦p¡A¦b¬d¸ß¸ê®Æ®w®É¡A­Y®É¶¡¹L¤[¡A§Ú­Ì³q±`§Æ±æ¯à°÷¦bºô­¶Åã¥Ü¡u¸ê®Æ³B²z¤¤...¡vµ¥¦r¼Ë¡A¥H§KÅý¨Ï¥ÎªÌ¥H¬°¬O»·ºÝ¦øªA¾¹·í¾÷¤F¡C¥H¤U³o­Ó²³æªº½d¨Ò¡A´N¯à¹F¨ì¦¹¥\¯à¡G

Example¡]ajax/ajaxViaPrototype03.htm¡^¡G

·í§Ú­ÌÂIÀ»¡u°e¥X¡v®É¡Aºô­¶·|¥ýÅã¥Ü¡u¸ê®Æ³B²z¤¤¡A½Ðµy«á...¡vµ¥¦r¼Ë¡A¦p¤U¡G

µ¥¬d¸ß§¹²¦«á¡A©Ò¦^¶Çªº¸ê®Æ´N·|Åã¥Ü¦bºô­¶¤W¡C¦¹½d¨Òªº­ì©l½X¦p¤U¡G

­ì©lÀÉ¡]ajax/ajaxViaPrototype03.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head>

<body>
<h2 align=center>AJAX Webpage Design Using Responders.register in Prototype.js</h2>
<hr>

<script src="prototype.js"></script>
<script>
// °e¥X¹ï¸ê®Æ®wªº¬d¸ß
function sendQuery(){
	// µù¥U AJAX ¬Y¨Ç¨Æ¥ó©Ò¹ïÀ³ªº¨ç¼Æ
	Ajax.Responders.register ({
		onLoading:
			function(){
				Element.update('queryResult', '<font color=red>¬d¸ß¸ê®Æ¤¤¡A½Ðµy­Ô...</font>');
			},
		onComplete:
			function(junk, xmlHttpObj){
				Element.update('queryResult', xmlHttpObj.responseText);
			}
		});
	var url = 'queryDb03.asp';				// ¦øªA¾¹ªºµ{¦¡ºô­¶	
	var queryString = 'singerName=' + $F('singerName');	// °Ñ¼Æ¦C
	var ajax = new Ajax.Request(url, {method:'post', parameters:queryString});
}
</script>

ºq¤â¤j¦W¡G<input type="text" id="singerName" value="¾HÄR§g">
<input type="button" onClick="sendQuery()" value="°e¥X">
<div id="queryResult"></div>

</body>
</html>

¦b¦¹½d¨Ò¤¤¡A§Ú­Ì¨Ï¥Î Ajax.Responders.register() ¨Óµù¥U¤F¨â­Ó¨ç¼Æ¡A¹ïÀ³¨ì onLoading ¨Æ¥óªº¨ç¼Æ¥i¥H¤£Â_¦L¥X¡u¸ê®Æ³B²z¤¤¡A½Ðµy«á...¡v¤§°T®§¡A¦Ó¹ïÀ³¦Ü onComplete ¨Æ¥óªº¨ç¼Æ«h¥i¥HÅã¥Ü³Ì«á¬d¸ßµ²ªG¡C


JScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©ó¦øªA¾¹ºÝªº ASP Àô¹Ò