Tutorials to .com

Tutorials to .com » Asp » Xml » Use XMLHTTP to obtain data without refresh.

Use XMLHTTP to obtain data without refresh.

Print View , by: iSee ,Total views: 15 ,Word Count: 777 ,Date: Wed, 22 Apr 2009 Time: 2:49 AM

Use XMLHTTP to achieve two linked Select.

This is the second "using XMLHTTP access to data without refresh" Another article on the application of XMLHTTP.
On XMLHTTP. It can be said that access is a very good mixture. The client and server to narrow the gap between the.
The use of XMLHTTP. We can achieve a lot of good ideas.

This article. 2 linked achieved Select.
Traditional 2-linked to all the data has reached the client ..
The use of XMLHTTP. We can return real-time data we need.


select.htm

<script language="JavaScript">
function GetResult (str)
(
/ *
*--------------- GetResult (str) -----------------
* GetResult (str)
* Function: send through the XMLHTTP request, return the results.
* Parameters: str, string, sending conditions.
* Examples: GetResult (document.all.userid.value);
* Author: wanghr100 (gray baby beans. Net)
* Update :2004-5-27 19:02
*--------------- GetResult (str) -----------------
* /
var oBao = new ActiveXObject ( "Microsoft.XMLHTTP");
oBao.open ( "POST", "Server.asp? sel =" + str, false);
oBao.send ();
/ / Server-side processing is returned to the escape-encoded string.
/ / Return data through the XMLHTTP to begin building a Select.
BuildSel (unescape (oBao.responseText), document.all.sel2)
)

function BuildSel (str, sel)
(
/ *
*--------------- BuildSel (str, sel) -----------------
* BuildSel (str, sel)
* Function: str build through Select.
* Parameters: str, string returned by the server. There is a specific structure "string 1, string 2, string 3"
* Parameters: sel, to build the Select
* Examples: BuildSel (unescape (oBao.responseText), document.all.sel2)
* Author: wanghr100 (gray baby beans. Net)
* Update :2004-5-27 19:02
*--------------- BuildSel (str, sel) -----------------
* /
/ / First empty the original data.
sel.options.length = 0;
var arrstr = new Array ();
arrstr = str.split (",");
/ / Start building the new Select.
for (var i = 0; i <arrstr.length; i + +)
(
sel.options [sel.options.length] = new Option (arrstr [i], arrstr [i])
)
)
</ script>
<select name="sel" onChange="GetResult(this.value)">
Please select <option value="">
<option value=""> Fujian
<option value=""> Hubei Province
<option value=""> Liaoning Province
<select>
<select name="sel2"> </ select>



server.asp server-side processing.

<% @ Language = "JavaScript"%>
<%
function OpenDB (sdbname)
(
/ *
*--------------- OpenDB (sdbname) -----------------
* OpenDB (sdbname)
* Functions: open database sdbname, return conn object.
* Parameters: sdbname, string, database name.
* Examples: var conn = OpenDB ( "database.mdb");
* Author: wanghr100 (gray baby beans. Net)
* Update :2004-5-12 8:18
*--------------- OpenDB (sdbname) -----------------
* /
var connstr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath (sdbname);
var conn = Server.CreateObject ( "ADODB.Connection");
conn.Open (connstr);
return conn;
)
var oConn = OpenDB ( "data.mdb");
var province = Request ( "sel");
var arrResult = new Array ();
var sql = "select city from china where province = '" + province +"'";
var rs = Server.CreateObject ( "ADODB.Recordset");
rs.Open (sql, oConn, 1,1);
while (! rs.EOF)
(
/ / Traverse all the appropriate data array arrResult Add.
arrResult [arrResult.length] = rs ( "city"). Value;
rs.MoveNext ();
)
/ / escape resolved XMLHTTP. Chinese to deal with.
/ / Array into a string. By, "" connection string.
Response.Write (escape (arrResult.join (",")));
%>


database design
data.mdb
Table china.
Field
Auto-id number
Text province
city text

Table: china data:
id province city
1, Fuzhou, Fujian Province
2, Xiamen City, Fujian Province
3, Quanzhou City, Fujian Province
4 Wuhan City, Hubei Province
5 Jingzhou City, Hubei Province
6 Yichang City in Hubei Province
7 in Shenyang City, Liaoning Province
8 in Dalian City, Liaoning Province
9, Panjin City, Liaoning Province


Asp and XML related Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.