Use php and ajax to write a three-level linkage between provinces, cities, and cities to implement regional drop-down selection,

Source: Internet
Author: User

Use php and ajax to write a three-level linkage between provinces, cities, and cities to implement regional drop-down selection,
To achieve three levels of interaction on this page, we need to create three php files. The first php file is imported into the jQuery file, which is embedded with JavaScript. The second php file is a php processing page, introduce the encapsulated database files. The third PHP file is the encapsulated database files. the first php JavaScript file

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script src = "jquery-1.11.2.min.js"> </script>
<Title> untitled document </title>
</Head>
<Br/>
<H1> all provinces, municipalities, and districts in China <Div id = "sj"> </div>

<Body>
<Script type = "text/javascript">

$ (Document). ready (function (e ){


// Create a drop-down menu in the div
$ ("# Sj" 2.16.html ("<select id = 'shanghai'> </select> <select id = 'shi'> </select> <select id = 'qu'> </select> ");


// Fill data with the Fill keyword
FillSheng (); // fill in provincial data
FillShi (); // fill the City Data
FillQu (); // fill area data

// Fill province Method
Function FillSheng (){

Var code = "0001"; // the province's parent code

$. Ajax ({

Url: "sheng_2_chuli.php ",
Data: {code: code },
Type: "POST ",
DataType: "TEXT ",
Async: false,
Success: function (data ){
Var hang = data. split ("| ");

Var str = "";

For (var I = 0; I

Var lie = hang [I]. split ("^ ");

Str + = "<option value = '" + lie [0] + "'>" + lie [1] + "</option> ";

}
// Put all <option> in the province drop-down list
$ ("# Sheng" 2.16.html (str );


}


});


}


// Fill the city
Function FillShi (){

Var code = $ ("# sheng"). val (); // the city's parent code

$. Ajax ({

Url: "sheng_2_chuli.php ",
Data: {code: code },
Type: "POST ",
DataType: "TEXT ",
Async: false,
Success: function (data ){

Var hang = data. split ("| ");

Var str = "";

For (var I = 0; I

Var lie = hang [I]. split ("^ ");

Str + = "<option value = '" + lie [0] + "'>" + lie [1] + "</option> ";

}
// Put all <option> in the drop-down list of the city
$ ("# Shi" pai.html (str );

}


});

 

}


// Method of filling Area
Function FillQu (){

Var code = $ ("# shi"). val (); // The parent code of the partition

$. Ajax ({
Url: "sheng_2_chuli.php ",
Data: {code: code },
Type: "POST ",
DataType: "TEXT ",
Async: false,
Success: function (data ){
Var hang = data. split ("| ");
Var str = "";

For (var I = 0; I Var lie = hang [I]. split ("^ ");

Str + = "<option value = '" + lie [0] + "'>" + lie [1] + "</option> ";


}
// Put all <option> in the drop-down list of the Zone
$ ("# Qu" pai.html (str );
}

 


});

}


// When you click to select the province, the city and district change together.
$ ("# Sheng"). change (function (){
FillShi ();
FillQu ();

});

// When you click to select the city, the zone changes together.
$ ("# Shi"). change (function (){
FillQu ();


});

 

});

 

 


</Script>


</Body>
</Html>

 

 

 

2. The second php processing file

<? Php
Include ("DBDA. class. php ");

$ Db = new DBDA ();

$ Pcode = $ _ POST ["code"];

$ SQL = "select AreaCode, AreaName from chinastates where ParentAreaCode = '{$ pcode }'";

Echo $ db-> StrQuery ($ SQL );
?>

 

 

 

3. The third php package database files

 

<? Php
Class DBDA
{
Public $ host = "localhost ";
Public $ uid = "root ";
Public $ pwd = "root ";
Public $ dbname = "mydb ";

 


Public function Query ($ SQL, $ type = 1)
{
$ Db = new mysqli ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname );
$ Result = $ db-> Query ($ SQL );
If ($ type = "1 ")
{
Return $ result-> fetch_all ();

 

} Else
{
Return $ result;
}
}

 


Public function StrQuery ($ SQL, $ type = 1)
{
$ Db = new mysqli ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname );
$ Result = $ db-> Query ($ SQL );
If ($ type = "1 ")
{
$ Arr = $ result-> fetch_all ();
$ Str = "";
Foreach ($ arr as $ v)
{
$ Str = $ str. implode ("^", $ v). "| ";
}
Return substr ($ str, 0, strlen ($ str)-1 );
} Else
{
Return $ result;
}
}
}

 

 

Effect:

 

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.