Background
The front-end page contains two select boxes, one associated with the school and the other associated with the major. Now you need to select the select box of the school to display the information about the school, the select professional only belongs to the professional name of the college. That is, to achieve a second-level linkage effect.
The onchange events are defined in the two select statements respectively. In the event, the ajax GET method is used to submit information to the backend PHP, and then the queried information is echo or document. write.
Note: The code is written by a blogger named y0umer.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var XmlHttp;
Function createXmlHttpRequestObject (){
If (window. ActiveXobject) {// determines whether it is an IE browser
Try {// start with try
Xmlhttp = new ActiveXobject ("Microsoft. XMLHTTP"); // use an ActiveX object to Create ajax
} Catch (e ){
XmlHttp = false;
} // Try end
} Else {// non-ie kernels such as Chrome and FireFox
Try {
XmlHttp = new XMLHttpRequest (); // It is considered non-ie
} Catch (e ){
XmlHttp = false; // other non-mainstream browsers
}
} // Judge the end. If the creation is successful, a DOM object is returned. If the creation is unsuccessful, a false object is returned.
If (xmlHttp)
{
Return xmlHttp;
} Else {
Alert ("An error occurred while creating the object. Please check whether the browser supports XmlHttpRequest! ");
}
} // Function body
// School drop-down list event
Function showCollegeInfo (){
Var selectIndex = document. getElementById ("college"). selectedIndex; // obtain the first few selected
Var value = document. getElementById ("college"). options [selectIndex]. value;
If (value)
{
// Create an object instance first
CreateXmlHttpRequestObject ();
// Use the event object to obtain the value of the text box ID
Var vCollege = value;
Var url = "college. php? Xy = "+ vCollege; // URL to be sent
Url = encodeURI (url );
XmlHttp. onreadystatechange = ajaxok; // determines the browser Status Bar (receives events triggered by playing data)
XmlHttp. open ("GET", url, false); // GET sends data to the server
XmlHttp. send (null );
Document. getElementById ("collegeinfo"). style. display = "block"; // div that displays the School Information
} Else {
Document. getElementById ("collegeinfo"). style. display = "none"; // hide the div of the School Information
}
}
Function ajaxok ()
{
If (xmlHttp. readyState = 4 & xmlHttp. status = 200)
{
Document. getElementById ("collegeinfo"). innerHTML = xmlHttp. responseText;
}
}
// Professional drop-down box events
Function showMajorInfo (){
Var selectIndex = document. getElementById ("major"). selectedIndex; // obtain the number of items selected
Var value = document. getElementById ("major"). options [selectIndex]. value;
If (value)
{
// Create an object instance first
CreateXmlHttpRequestObject ();
// Use the event object to obtain the value of the text box ID
Var vMajor = value;
Var url = "major. php? Zy = "+ vMajor; // URL to be sent
Url = encodeURI (url );
XmlHttp. onreadystatechange = ajaxok2; // judge the browser Status Bar (receives events triggered by playing data)
XmlHttp. open ("GET", url, false); // GET sends data to the server
XmlHttp. send (null );
Document. getElementById ("majorinfo"). style. display = "block"; // div that displays professional information
} Else {
Document. getElementById ("majorinfo"). style. display = "none"; // div that hides professional information
}
}
Function ajaxok2 ()
{
If (xmlHttp. readyState = 4 & xmlHttp. status = 200)
{
Document. getElementById ("majorinfo"). innerHTML = xmlHttp. responseText;
}
}
</Script>