Ajax cascading selection Box

Source: Internet
Author: User

Ajax cascading selection Box

Cascading selection box Common and more responsible for web development, such as the implementation of the Product add page, you need to select the classification of goods, and classification information has a hierarchy, such as large classification and small classification is two-level, when the user selects the product belongs to the large category, the content of its own small category needs to be replaced by large categories of classified content.

Code implementation;

Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Send (args)
Args: This parameter is the requested parameter information, which is a string that can be used to concatenate multiple parameters using the "&" symbol.
Xmlhttp.open ("POST", "typeservice.jsp", true); Make an HTTP request
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("type=" +type);
<script language= "JavaScript" >
var xmlHttp;
function Createrequest (type) {
Initialize the object and issue a XMLHttpRequest request
XmlHttp = false;
if (window. XMLHttpRequest) {//browser other than IE
XmlHttp = new XMLHttpRequest ();
} else if (window. ActiveXObject) {//IE browser
try {
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
} catch (e) {
try {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!xmlhttp) {
Alert ("Cannot create XMLHTTP instance!");
return false;
}
Xmlhttp.onreadystatechange = insertcontents; Specify a method for handling response results
Xmlhttp.open ("POST", "typeservice.jsp", true); Make an HTTP request
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("type=" +type);
}
function insertcontents () {//processing the information returned by the server
if (xmlhttp.readystate = = 4) {
if (Xmlhttp.status = = 200) {
Subtype.innerhtml=xmlhttp.responsetext;
} else {
Alert (' The page you requested was found to be wrong ');
}
}
}
</script>
<body>
<table width= "border=" 0 "align=" center "cellpadding="-2 "cellspacing="-2 "bordercolordark=" #FFFFFF ">
<form action= "" method= "Post" Name= "Form1" >
<tr>
&LT;TD width= "20%" height= ">" Product name:</td>
&LT;TD height= "colspan=" 3 ">&nbsp;
<input name= "Goodsname" type= "text" class= "Sytle_text" id= "bookID2" size= ">"
&nbsp;&nbsp; </td>
</tr>
<tr>
&LT;TD height= "Qi" > Belongs to large class:</td>
&LT;TD width= "35%" height= ">&nbsp;"
<select name= "type" class= "textarea" id= "type" onchange= "Createrequest (this.value)" >
<%
Jdbcdao dao = new Jdbcdao ();
list<string> types = Dao.gettypes ();
for (String type:types) {
Out.println ("<option value= '" +type+ ">" + Type + "</option>");
}
%>
</select></td>
&LT;TD width= "18%" height= ">" category:</td>
&LT;TD width= "42%" height= "id=" >&nbsp;</td>
</tr>
<tr>
&LT;TD height= "Photos" > Photo files:</td>
&LT;TD height= ">&nbsp;"
<input name= "Picture" type= "text" class= "Style_upload" id= "Picture" >
</td>
&LT;TD height= "$" > Pricing:</td>
&LT;TD height= ">&nbsp;"
<input name= "Price" size= "8" type= "text" class= "Sytle_text" id= "Price" >
(Yuan) </td>
</tr>
<tr>
&LT;TD height= "103" > Product introduction:</td>
&LT;TD colspan= "3" ><span class= "Style5" >&nbsp; </span>
<textarea name= "introduce" cols= "" rows= "5" class= "textarea" id= "introduce" ></textarea></td>
</tr>
<tr>
&LT;TD height= "colspan=" 4 "align=" center "><input name=" button "type=" button "
class= "Btn_grey" value= "Save" onclick= "MyCheck ();" >
&nbsp;
<input name= "Submit2" type= "reset" class= "Btn_grey" value= "reset" >
&nbsp;</td>
</tr>
</form>
</table>
<script language= "JavaScript" >
Createrequest (Form1.type.value);
</script>
</body>
<%
Request.setcharacterencoding ("UTF-8");
String type = Request.getparameter ("type");
Jdbcdao dao = new Jdbcdao ();
list<string[]> subtypes = dao.getsubtypes (type);
%>
<select name= "TypeID" class= "textarea" id= "TypeID" >
<%
For (String subtype[]: subtypes) {
%>
<option value= "<%=subType[0]%>" ><%=subType[1]%></option>
<%}
%>
</select>
Public list<string> GetTypes () {
List types=new ArrayList (); Create a list collection
Statement stmt = null;
ResultSet rs = null; Creating a JDBC Result set object
Connection conn = Getconn (); Connecting to a database
try {
Defining SQL query statements
String sql = "Select type from Tb_type GROUP by type";
stmt = Conn.createstatement ();
rs = stmt.executequery (SQL); Execute the SQL query and get the result set
while (Rs.next ()) {//Traversal result set
String type = rs.getstring (1);
Types.add (type); Add large classification information to the list collection
}
} catch (SQLException ex) {
Logger.getlogger (GetClass (). GetName ()). log (Level.severe, NULL, ex);
} finally {
Close (RS, stmt, conn); Whether the JDBC Resource
}
return types; Returns a list collection that holds large classification information
}
Public list<string[]> getsubtypes (String type) {
List subtypes=new ArrayList (); Create a list collection
Statement stmt = null;
ResultSet rs = null; Creating a JDBC Result set object
Connection conn = Getconn (); Connecting to a database
try {
Defining SQL query statements
String sql = "Select Id,subtype from Tb_type where type= '" +type+ "'";
stmt = Conn.createstatement ();
rs = stmt.executequery (SQL); Execute the SQL query and get the result set
while (Rs.next ()) {//Traversal result set
Using arrays to save small-class information
String subtype[] = {rs.getstring (1), rs.getstring (2)};
Subtypes.add (subtype); Add a small categorization to the list collection
}
} catch (SQLException ex) {
Logger.getlogger (GetClass (). GetName ()). log (Level.severe, NULL, ex);
} finally {
Close (RS, stmt, conn); Whether the JDBC Resource
}
return subtypes; Returns a list collection of small categorization information
}

Ajax cascading selection Box

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.