Out-of-office starter php + mysql uses ajax technology for provincial, municipal, and prefecture-level 3 linkage without refreshing menu source code

Source: Internet
Author: User

Absolute Originality
Test address: http://www.mlmm.cn/mypage? Name = ceshi

Test Page code:Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> php + ajax dynamically generated drop-down menu </title>
<Script language = "JavaScript" src = "../include/js/regions. js"> </script>
</Head>
<Body>
<Form name = "form1" method = "post" action = "">
<Tr> <td >{$ regionss (People's Republic of China)} </td> </tr>
</Form>
</Body>
</Html>

Regionss function code:
QUOTE:Copy codeThe Code is as follows: // provincial/municipal level 3 linkage drop-down menu function regionss provincial/municipal/local
Function regionss ($ country = "People's Republic of China ")
{
Global $ db; $ I = 1;
$ Text = "<select id = \" select1 \ "name = \" select1 \ "onchange = \" startRequest () \ "style = \" width: 90px; \ "> <option value = \" \ "> province/city/Autonomous Region </option> \ n ";

$ Result = $ db-> query ("SELECT province FROM". TABLE_PROVINCE. "WHERE country = '$ country' order by provinceid ");
While ($ r = $ db-> fetch_array ($ result ))
{
$ Text. = "<option value =". $ r ['province ']. ">". $ r ['province']. "</option> ";
$ I ++;
}
$ Text. = "</select> ";
$ Text. = "& nbsp <select id = \" select2 \ "name = \" select2 \ "onchange = \" startRequesta () \ "style = \" width: 90px; \ "> <option value = \" \ "> city/county/district </option> </select> \ n ";
$ Text. = "<select id = \" select3 \ "name = \" select3 \ "style = \" width: 90px; \ "> <option value = \" \ "> county/county </option> </select> \ n ";
Return $ text;

}

Regions. js code:

Copy codeThe Code is as follows: // var AB = new Array ();
Var xmlHttp;
Var xmlHttpa;
Function createXMLHttpRequest (){
If (window. ActiveXObject ){
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
}
}
Function createXMLHttpRequesta (){
If (window. ActiveXObject ){
XmlHttpa = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest ){
XmlHttpa = new XMLHttpRequest ();
}
}

Function startRequest (){
CreateXMLHttpRequest ();
CreateXMLHttpRequesta ();
XmlHttp. onreadystatechange = handleStateChange;
XmlHttpa. onreadystatechange = handleStateChangearea;
Document. getElementById ('select2'). options. length = 0;
Document. getElementById ('select3 '). options. length = 0;
Var url = document. form1.select1. value;
Var qurl = "/regions. php? Province = "+ url +" & time = "+ new Date (). getTime ();
XmlHttp. open ("GET", qurl, true );
XmlHttp. send (null );
// SetTimeout ("startRequest ()", 2000 );
}

Function handleStateChange (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Var obj = document. getElementById ('select2'); // write the string returned by the server to the area where the ID is select2 on the page.
Obja = document. getElementById ('select3 '); // write the string returned by the server to the area where the ID is select3 on the page.
Eval (xmlHttp. responseText );

}
}
}
Function handleStateChangearea (){
Eval (xmlHttpa. responseText );
}
// Var AB = new Array ();

Function startRequesta (){
CreateXMLHttpRequesta ();
XmlHttpa. onreadystatechange = handleStateChangea;
Document. getElementById ('select3 '). options. length = 0;
Var url = document. form1.select2. value;
Var qurl = "/regions. php? City = "+ url +" & time = "+ new Date (). getTime ();
XmlHttpa. open ("GET", qurl, true );
XmlHttpa. send (null );
// SetTimeout ("startRequest ()", 2000 );
}

Function handleStateChangea (){
If (xmlHttpa. readyState = 4 ){
If (xmlHttpa. status = 200 ){
Var obja = document. getElementById ('select3 ');
Eval (xmlHttpa. responseText );
// Write the string returned by the server to the area with ID select3 on the page

}
}
}

Data island regions. php Page code

Copy codeThe Code is as follows: <? Php
Require "common. php ";
$ City = $ city? $ City :"";
$ Area = $ area? $ Area :"";
If ($ province & $ city = ''){
Global $ db; $ I = 1;

$ Result = $ db-> query ("select distinct city FROM". TABLE_CITY. "WHERE province = '$ province' order by cityid ");
While ($ r = $ db-> fetch_array ($ result ))
{
$ R [city] = iconv ('gb2312', 'utf-8', $ r [city]);
Echo "obj. options [obj. options. length] = new Option ('". $ r [city]. "','". $ r [city]. "'); \ n ";
$ I ++;
}
$ Resultarea = $ db-> query ("select distinct city FROM". TABLE_CITY. "WHERE province = '$ province' order by cityid ");
$ R = $ db-> fetch_array ($ resultarea );
$ City = $ r [city];
$ Resulta = $ db-> query ("select distinct area FROM". TABLE_CITY. "WHERE city = '$ City' order by cityid ");
While ($ ra = $ db-> fetch_array ($ resulta ))
{
$ Ra [area] = iconv ('gb2312', 'utf-8', $ ra [area]);
Echo "obja. options [obja. options. length] = new Option ('". $ ra [area]. "','". $ ra [area]. "'); \ n ";
$ I ++;
}

}
If ($ city & $ province = ''){
Global $ db; $ I = 1;

$ Result = $ db-> query ("select distinct area FROM". TABLE_CITY. "WHERE city = '$ City' order by cityid ");
While ($ r = $ db-> fetch_array ($ result ))
{
$ R [area] = iconv ('gb2312', 'utf-8', $ r [area]);
Echo "obja. options [obja. options. length] = new Option ('". $ r [area]. "','". $ r [area]. "'); \ n ";
$ I ++;
}
}
?>

Address information database:

Phpcms3.0 database is used, and I have not made any changes

This program can be used directly in phpcms3.0. If it is used elsewhere, modify it by yourself.

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.