Two-level linkage menu based on ASP + Ajax and database driver

Source: Internet
Author: User

Index. asp page Code Copy code The Code is as follows: <! -- # Include file = "conn. asp" -->
<%
Set cmd = conn.exe cute ("select bigclassid, bigclassname from bigclass ")
Tempid = cmd ("bigclassid ")
%>
<Select name = "menu" onchange = "getsubcategory (this. Value);">

<%
if not cmd. EOF then
do while not cmd. EOF
bigclassid = cmd ("bigclassid")
bigclassname = cmd ("bigclassname ")
%>
<% = bigclassname %>
<%
cmd. movenext
loop
end if
cmd. close
set cmd = nothing
%>


<%
set cxd = conn.exe cute ("select * From smallclass where bigclassid =" & tempid)
if not cxd. EOF then
do while not cxd. EOF
smallclassid = cxd ("smallclassid")
smallclassname = cxd ("smallclassname ") %>
<% = smallclassname %>
<%
cxd. movenext
loop
cxd. close
set cxd = nothing
else
html = " no small class "
response. write HTML
end if
%>

Ajax. JS CodeCopy codeThe Code is as follows: // JavaScript document
Function createxmlhttp ()
{
Xmlhttpobj = false;
Try {
Xmlhttpobj = new XMLHttpRequest;
} Catch (e ){
Try {
Xmlhttpobj = new activexobject ("msxml2.xmlhttp ");
} Catch (E2 ){
Try {
Xmlhttpobj = new activexobject ("Microsoft. XMLHTTP ");
} Catch (E3 ){
Xmlhttpobj = false;
}
}
}
Return xmlhttpobj;
}

Function getsubcategory (bigclassid ){
If (bigclassid = 0 ){
Document. getelementbyid ("subclass "). innerhtml = "<select name = 'smallclassid '> <option value = '0' selected> Select secondary category </option> </SELECT> ";
Return;
};
VaR xmlhttpobj = createxmlhttp ();
If (xmlhttpobj) {// if the object xmlhttpobj is created successfully
Xmlhttpobj. onreadystatechange = handle;
Xmlhttpobj. Open ('get', "getsubcategory. asp? Bigclassid = "+ bigclassid +" & number = "+ math. Random (), true); // Add a random number to the get method.

Xmlhttpobj. Send (null );
}
}

Function handle () {// client monitoring function
// If (xmlhttpobj. readystate = 4) {// The server completes processing the request
If (xmlhttpobj. Status = 200 ){
// Alert ('OK ');
VaR html = xmlhttpobj. responsetext; // obtain the returned value
Document. getelementbyid ("subclass"). innerhtml = HTML;
} Else {
Document. getelementbyid ("subclass"). innerhtml = "sorry, the requested page has a problem ...";
}
//}
// Else {
// Document. getelementbyid ("subclass"). innerhtml = xmlhttpobj. readystate; // the server is processing
//}
//}

}

getsubcategory. ASP code copy Code the code is as follows: <% @ Language = "VBScript" codePage = "936" %>

<%
response. charset = "gb2312"
bigclassid = safe (request. querystring ("bigclassid")
If bigclassid <> "" Then
set Re = new Regexp
re. ignorecase = true
re. global = false
re. pattern = "^ [0-9] {1, 3} $"
if not re. test (bigclassid) Then
response. write "invalid parameter"
response. end
end if %>

<% on error resume next
Set P = conn.exe cute ("select * From smallclass where bigclassid =" & bigclassid)
If err then
err. clear
response. write "query error"
response. end
end if
if not p. EOF then
html = "" & vbnewline do while not p. EOF html = HTML & "" & P ("smallclassname ") & "" & vbnewline P. movenext loop html = HTML & ""
else
html = " no small class "
end if
P. close
Set P = nothing
Conn. close
set conn = nothing
response. write HTML
html = ""
end if
%>

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.