Cascade menu [ajax + asp]

Source: Internet
Author: User

<%
Dim conn, rs
Dim connstr
Dim sqlCmd
'Create a database connection object and open
Set conn = server. createobject ("adodb. connection ")
Connstr = "Provider = Microsoft. jet. oledb.4.0; data source =" & server. mappath ("data. mdb ")
Conn. open connstr
'SQL statement used to obtain data from the database
SqlCmd = "select id, name from category where level = 0"
'Create a dataset object
Set rsw.conn.exe cute (sqlCmd)
%>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<Html>
<Head>
<Title> list menu example </title>
</Head>
<Body>
<Form name = "form1">
<Select name = "select1" onchange = "getLevel2 ()">
<Option value = "0"> select a level-1 Category: </option>
<%
'Retrieve record set generation drop-down menu option
While not rs. eof
Response. write ("<option value = '" & rs ("id") & "'> ")
Response. write (rs ("name "))
Response. write ("</option> ")
Rs. movenext
Wend
'Close database connection and record set and release resources
Rs. close
Conn. close
Set rs = nothing
Set conn = nothing
%>
</Select>
<Select name = "select2">
<Option value = "0"> Select Level 2: </option>
</Select>
</Form>
</Body>
</Html>
<Script language = "JavaScript" type = "text/javascript">
<! --
// Cache is used to store the acquired data
Var cache = [];
Function getLevel2 (){
If (document. forms. form1.select1. selectedIndex = 0 ){
// When the level-1 menu is not selected, the level-2 menu only retains the prompt item
Document. forms. form1.select2. length = 1;
Return;
 }
// If the current level-2 category is not cached, it is obtained from the server.
If (! Cache [document. forms. form1.select1. selectedIndex]) {
// Create a cross-browser XMLHttpRequest object
Var xmlhttp;
Try {
Xmlhttp = new ActiveXObject ('msxml2. Xmlhttp ');
} Catch (e ){
Try {
Xmlhttp = new ActiveXObject ('Microsoft. Xmlhttp ');
} Catch (e ){
Try {
Xmlhttp = new XMLHttpRequest ();
} Catch (e ){}
   }
  }
// Create a request and use escape to encode userName to avoid garbled characters
Xmlhttp. open ("get", "ajax. asp? Id = "+ document. forms. form1.select1. value );
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4 ){
If (xmlhttp. status = 200 ){
Cache [document. forms. form1.select1. selectedIndex] = eval ("(" + unescape (xmlhttp. responseText) + ")");
// Call getLevel2 again after obtaining the data, and fill the data in the drop-down box. If you write the data directly here, the code will be duplicated.
GetLevel2 ();
} Else {
Alert ("network failed. ");
    }
   }
  }
Xmlhttp. send (null );
// It must be returned here, waiting for callback
Return;
 }
// Make sure that the cache is not empty.
Document. forms. form1.select2. length = 1;
Var _ arr = cache [document. forms. form1.select1. selectedIndex];
For (var I = 0; I <_ arr. length-1; I ++ = 2 ){
With (document. forms. form1.select2 ){
Options [options. length] = new Option (_ arr [I], _ arr [I + 1]);
  }
 }
}
// -->
</Script>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.