Php implements a three-level Cascade drop-down box,

Source: Internet
Author: User

Php implements a three-level Cascade drop-down box,

This is the information on the three-level Cascade drop-down box for php I found on the Internet. Let's share the information and make progress together. The details are as follows:

Index. php:

<Html> 

Aa. js

<! -- Var xmlHttpvar xmlHttp2 // function showMenu (str)-function 1 function showMenu (str) xmlHttp = GetXmlHttpObject () // call the GetXmlHttpObject () function, create XMLHttpRequest object if (xmlHttp = null) // if creation fails, alert ("Browser does not support HTTP Request") return var url = "get2.php" // define the url, it mainly reads Database url = url + "? Q = "+ strurl = url +" & sid = "+ Math. random () // the random method of the Math object, and the random number xmlHttp. onreadystatechange = stateChanged // sets the function called when the XMLHttpRequest object xmlHttp status changes. Do not add parentheses xmlHttp after the function name. open ("GET", url, true) // use the open () method of the XMLHttpRequest object to create an HTTP request xmlHttp. send (null) // use the open () method of the XMLHttpRequest object to send an HTTP request} // function showMenu (str)-function 1 function showMenu2 (str) {xmlHttp2 = GetXmlHttpObject () // call the GetXmlHttpObject () function to create the XMLHttpRequest object I F (xmlHttp2 = null) // If creation fails, alert ("Browser does not support HTTP Request") return var url = "get2.php" // defines the url, it mainly reads Database url = url + "? Q2 = "+ strurl = url +" & sid = "+ Math. random () // the random method of the Math object, and the random number xmlHttp2.onreadystatechange = stateChanged2 // sets the function called when the XMLHttpRequest object xmlHttp status changes, do not add parentheses (xmlHttp2.open ("GET", url, true) after the function name. // use the open () method of the XMLHttpRequest object to create an HTTP request xmlHttp2.send (null) // use the open () method of the XMLHttpRequest object to send an HTTP request} // function BuildSel () based on the returned string, recreate the new drop-down menu control Select-function 2 function BuildSel (str, sel) // clear the original data first. sel. options. length = 0; var arrstr = new Array (); arrstr = str. split (","); if (str. length> 0) // loop for (var I = 0; I <arrstr. length; I ++) var subarrstr = new Array (); subarrstr = arrstr [I]. split ("|"); sel. options. add (new Option (subarrstr [1], subarrstr [0]); sel. options [0]. selected = true;} // function stateChanged ()-responds to HTTP Request status change function stateChanged () // determines whether the value of the readyState attribute of the XMLHttpRequest object is 4, if the value is 4, the asynchronous call is completed (note: the asynchronous call is not successful) if (xmlHttp. readyState = 4 | xmlHttp. readyState = "complete") // If the asynchronous call is successful --> the value of the status attribute of the XMLHttpRequest object is 200, and the html file is running locally, xmlHttp. the returned value of status is 0, so xmlHttp should be added. status = 0 if (xmlHttp. status = 200 | xmlHttp. status = 0) // call the getElementById () method of the DOM of the Document Object Model to find the tag txtHint in the html file. // innerHTML is the attribute in the IE browser, it can be used to change text content between tags, // xmlHttp. responseText: Get the data responseText through the responseText attribute of XMLHttpRequest, and the result is a string; responseXML, the result is in XML format // document. getelementbyidx_xx_x_xx ("txtHint "). innerHTML = xmlHttp. responseText BuildSel (xmlHttp. responseText, document. getElementsByTagName_r ("*"). sel2) showMenu2 (document. getElementsByTagName_r ("*"). sel2.value) ;}/// function stateChanged ()-responds to HTTP Request status change function stateChanged2 () // determines whether the value of the readyState attribute of the XMLHttpRequest object is 4, if the value is 4, the asynchronous call is completed (note: the asynchronous call is not successful) if (xmlHttp2.readyState = 4 | xmlHttp2.readyState = "complete ") // If the asynchronous call is successful --> the value of the status attribute of the XMLHttpRequest object is 200, and the html file runs locally, xmlHttp. the returned value of status is 0, so xmlHttp should be added. status = 0 if (xmlHttp2.status = 200 | xmlHttp2.status = 0) // call the getElementById () method of the DOM of the Document Object Model to find the tag txtHint in the html file, // innerHTML is an attribute in IE browser and can be used to change text content between tags. // xmlHttp. responseText: Get the data responseText through the responseText attribute of XMLHttpRequest, and the result is a string; responseXML, the result is in XML format // document. getelementbyidx_xx_x_xx ("txtHint "). innerHTML = xmlHttp. responseText BuildSel (xmlHttp2.responseText, document. getElementsByTagName_r ("*"). sel3) }}// function GetXmlHttpObject ()-create an XMLHttpRequest object, that is, create an asynchronous call object function GetXmlHttpObject () var xmlHttp = null; try // Firefox, Opera 8.0 +, safari xmlHttp = new XMLHttpRequest (); catch (e) // Internet Explorer try xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP"); catch (e) xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");} return xmlHttp; -->

Get2.php

<? Php header ('content-Type: text/html; charset = GB2312 '); $ conn = mysql_connect ('localhost', 'root ','') or die ('could not connect :'. mysql_error (); mysql_select_db ('fitment ', $ conn) or die ('can \' t use database :'. mysql_error (); mysql_query ("set names 'gb2312'"); // If the passed qif ($ _ GET ["q"]) {$ strs = "SELECT * FROM fitment_sort WHERE supid = '". $ _ GET ["q"]. "'"; // echo $ str; $ results = mysql_query ($ strs) or Die ("Invalid query :". mysql_error (); if ($ results) while ($ arrs = mysql_fetch_array ($ results) {$ strings. = $ arrs ["id"]. "| ". $ arrs ["name"]. ",";} echo substr ($ strings, 0, strlen ($ strings)-1);} // If the passed q2if ($ _ GET ["q2"]) {$ str = "SELECT * FROM fitment_sort WHERE supid = '". $ _ GET ["q2"]. "'"; // echo $ str; $ result = mysql_query ($ str) or die ("Invalid query :". mysql_error (); if ($ result) while ($ arr = mysql_fe Tch_array ($ result) {$ string. = $ arr ["id"]. "| ". $ arr ["name"]. ",";} echo substr ($ string, 0, strlen ($ string)-1);} mysql_close ($ conn);?>

The above is the code for php to implement a three-level Cascade drop-down box. I hope it will be helpful for you to learn about php programming.

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.