Ajax + php second-level linkage menu code
<Script language = "javascript">
Var http_request = false;
Function send_request (url) {// initialization, specifying the processing function and sending the request function
Http_request = false;
// Start initializing the XMLHttpRequest object
If (window. XMLHttpRequest) {// Mozilla browser
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType) {// sets the MIME category
Http_request.overrideMimeType ("text/xml ");
}
}
Else if (window. ActiveXObject) {// IE browser
Try {
Http_request = new ActiveXObject ("Msxml2.XMLHttp ");
} Catch (e ){
Try {
Http_request = new ActiveXobject ("Microsoft. XMLHttp ");
} Catch (e ){}
}
}
If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. alert ("An error occurred while creating the XMLHttp object! ");
Return false;
}
Http_request.onreadystatechange = processrequest;
// Determine the request sending method, URL, and whether to execute the following code synchronously
Http_request.open ("GET", url, true );
Http_request.send (null );
}
// Function for processing the returned information
Function processrequest (){
If (http_request.readyState = 4) {// judge the object status
If (http_request.status = 200) {// The message is returned successfully. Start to process the information.
Document. getElementById (reobj). innerHTML = http_request.responseText;
}
Else {// The page is abnormal.
Alert ("the page you requested is abnormal! ");
}
}
}
Function getclass (obj ){
Var pid = document. form1.select1. value;
Document. getElementById (obj). innerHTML = "<option> loading... </option> ";
Send_request ('doclass. php? Pid = '+ pid );
Reobj = obj;
}
</Script>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> ajax + php second-level linkage menu code </title>
</Head>
<Body>
<Form name = "form1">
<Select name = "select1" id = "class1" style = "width: 100;" onChange = "getclass ('class2');">
<Option selected = "selected"> </option>
<Option value = "1"> Category 1 </option>
<Option value = "2"> Category 2 </option>
</Select>
<Select name = "select2" id = "class2" style = "width: 100;">
</Select>
</Form>
</Body>
</Html>
Php processing code
<? Php
Header ("Content-type: text/html; charset = GBK"); // output encoding to avoid Chinese garbled characters
$ Pid = $ _ GET ['pid'];
$ Db = mysql_connect ("localhost", "root", "7529639"); // create a database connection
Mysql_query ("set names 'gbk '");
Mysql_select_db ("menuclass ");
$ SQL = "select classname from menu where parentid =". $ pid ."";
$ Result = mysql_query ($ SQL );
// Cyclically listing options
While ($ rows = mysql_fetch_array ($ result )){
Echo '<option> ';
Echo $ rows ['classname'];
Echo "</option> n ";
}
?>