Asp+ajax and database-driven two-level linkage menu

Source: Internet
Author: User

<% @language = "VBScript" codepage= "936"%>
<!--#include file= "conn.asp Tutorial"-->
<%
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 "Illegal parameters"
Response.End
End If%>

<%on Error Resume Next
Set P = Conn.execute ("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 = "<select name= ' Select2 ' >" &vbnewline
Do as not p.eof
html = html& "<option value=" &p ("Smallclassid") & "' >" &p ("Smallclassname") & "</option > "&vbnewline
P.movenext
Loop
html = html& "</select>"
Else
html = "<select name= ' smallclassid ' ><option value= ' 0 ' selected> no small category </option></select>"
End If
P.close
Set P = Nothing
Conn.close
Set conn = Nothing
Response.Write html
html = ""
End If
%>
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 Class Two </option></select> ";
Return
};
var xmlhttpobj = Createxmlhttp ();
if (xmlhttpobj) {//If the object Xmlhttpobj successfully created
Xmlhttpobj.onreadystatechange=handle;
Xmlhttpobj.open (' Get ', ' getsubcategory.asp?bigclassid= ' +bigclassid+ ' &number= ' +math.random (), true);//get method Add a random number.


Xmlhttpobj.send (NULL);
}
}

function handle () {//Client monitoring functions
if (xmlhttpobj.readystate==4) {//server processing request complete
if (xmlhttpobj.status==200) {
Alert (' OK ');
var html = xmlhttpobj.responsetext;//Get return value
document.getElementById ("Subclass"). innerhtml=html;
}else{
document.getElementById ("Subclass"). Innerhtml= "Sorry, there is a problem with the page you requested ...";
}
//}
else{
document.getElementById ("Subclass"). innerhtml=xmlhttpobj.readystate;//Server Processing
//}
//}

}


<!--#include file= "conn.asp"-->
<%
Set cmd = Conn.execute ("Select Bigclassid,bigclassname from Bigclass")
Tempid=cmd ("Bigclassid")
%>
<select name= "Menu" onchange= "Getsubcategory (this.value);" >

<%
If not cmd.eof then
does while not cmd.eof
bigclassid= cmd ("Bigclassid")
Bigclassname = cmd ("Bi Gclassname ")
%>
<option value=" <%=bigclassid%> "><%=bigclassname%></option>
<%
Cmd.movenext
Loop
End If
cmd.close
Set cmd = Nothing
%>
</select>
& Lt;div id= "Subclass" >
<select name= "submenu"

<%
Set cxd = Conn.execute ("select * from Smallclass where bigclassid=" & tempid)
If not cxd.eof then br> do cxd.eof
smallclassid= cxd ("Smallclassid")
Smallclassname = Cxd ("Smallclassname")%>
< Option value= "<%=smallclassid%>" ><%=smallclassname%></option>
<%
Cxd.movenext
Loop
Cxd.close
Set cxd = Nothing
Else
HTML = "<select name= ' smallclassid ' ><option ' value= ' 0 ' s Elected> no small class </option></select>
Response.Write HTML
End If
%>
</select>
& Lt;/div>

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.