Ajax + php second-level linkage menu code

Source: Internet
Author: User

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 ";
  }

?>

Related Article

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.