Thinkphp + ajax implementation of regional linkage effect example

Source: Internet
Author: User
Tags foreach

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:

 

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.