As the project needs to write a regional linkage function, I found an address library on the Internet and put it in the database for a long time and finally realized the desired function. Let's talk about the source code.
Find provincial cities
"Run the code:
| The code is as follows: |
Copy code |
|
<? Php
$ Area = M ("Area ");
$ Privince = $ area-> where ("region_type = 1")-> select ();
$ This-> assign ("privince", $ privince );
?> |
Javascript code:
| The code is as follows: |
Copy code |
|
<Script type = "text/javascript">
Var xmlHttp;
Var requestType = "";
Function createXMLHttpRequest ()
{
If (window. ActiveXObject)
{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
XmlHttp = new XMLHttpRequest ();
}
}
Function handleStateChange (){
If (xmlHttp. readyState = 4)
{
If (xmlHttp. status = 200)
{
If (requestType = "city "){
Showcity ();
}
Else if (requestType = "area "){
Showarea ();
}
}
}
}
Function queryCity (citycode ){
CreateXMLHttpRequest ();
RequestType = "city ";
Var url = "/esoxue. php/Crm/area/provincecode/" + citycode + "/" + Math. random ();
XmlHttp. open ("GET", url, true );
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. send (null );
}
Function queryArea (citycode ){
CreateXMLHttpRequest ();
RequestType = "area ";
Var url = "/esoxue. php/Crm/area/citycode/" + citycode + "/" + Math. random ();
XmlHttp. open ("GET", url, true );
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. send (null );
}
Function showcity (){
Document. getElementById ("city"). innerHTML = xmlHttp. responseText; // capture ID to display the returned data
}
Function showarea (){
Document. getElementById ("area"). innerHTML = xmlHttp. responseText; // capture ID to display the returned data
}
</Script>
Html code:
<Div class = "controls">
<Select name = "area []" onchange = 'querycity (this. options [this. selectedIndex]. value) '>
<Option value = "0"> select a region </option>
<Volist name = "privince" id = "vo">
<Option value = "{$ vo. region_id}" >{$ vo. region_name} </option>
</Volist>
</Select> & nbsp;
<Span id = 'city'> </span> & nbsp;
<Span id = 'region'> </span>
</Div>
Run the code:
Public function area ()
{
$ Area = M ('area ');
$ Provincecode = $ _ GET ['provincecode']; // receives the provincial key value
$ Citycode = $ _ GET ['citycode']; // receives the city key value
If ($ provincecode! = ""){
$ Citys = $ area-> where ("parent_id = '$ provincecode'")-> select ();
Echo "<select name = 'area [] 'onchange = 'queryarea (this. options [this. selectedIndex]. value) '> ";
Echo "<option value = '-1' selected> Select City </option> n ";
Foreach ($ citys as $ key => $ val ){
Echo "<option value = '{$ citys [$ key] ['region _ id']}'> {$ citys [$ key] ['region _ name']} </option> ";
}
}
If ($ citycode! = ""){
$ Areas = $ area-> where ("parent_id = '$ citycode'")-> select ();
If (! Empty ($ areas )){
Echo "<select name = 'area [] '> n ";
Echo "<option value = '-1' selected> select a county </option> n ";
Foreach ($ areas as $ key => $ val ){
Echo "<option value = '{$ areas [$ key] ['region _ id']}'> {$ areas [$ key] ['region _ name']} </option> ";
}
Echo "</select> n ";
} Else {
$ Areaname = $ area-> where ("region_id = '$ citycode'")-> field ('region _ name')-> find ();
Echo "<select name = 'area [] '> n ";
Echo "<option value = '-1' selected> select a county </option> n ";
Echo "<option value = '{$ areaname ['region _ name']}' selected >{$ areaname ['region _ name']} </option> ";
Echo "</select> n ";
}
}
} |
The effect is as follows:
Final effect: