Php+mysql using AJAX technology, provincial and municipal 3-level linkage without refreshing menu source

Source: Internet
Author: User
Tags eval

It's simple.

1. First put<script language= "JavaScript" src= ". /include/js/regions.js "></script>
Add to Header.html page
2. ToRegionss ()function added to/include/tag.php.
3. Create a Web site in the root directoryregions.phpFile
4. Join in the appropriate place of your template{$regionss (People's Republic of China)}Can

Note: The name of the form can be changed according to your needs ...


Test page code:
<meta http-equiv= "Content-type" C/>
<title>php+ajax Dynamic Generation Pull-down Menu </title>
<script language= "JavaScript" src= ". /include/js/regions.js "></script>
<body>
<form name= "Form1" method= "Post" action= "" >
<tr><td>{$regionss (People's Republic of China)}</td></tr>
</form>
</body>

Regionss function Code:
Provincial and municipal 3-level Linkage drop-down menu function Regionss province. City.
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. = "<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=\" \ "> City/County </ Option></select>\n ";
return $text;

}

Code for Regions.js:

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 '); Writes the string returned by the server to the page with an ID select2 area
Obja = document.getElementById (' select3 '); Writes the string returned by the server to the page with an ID select3 area
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);
Writes the string returned by the server to the page with an ID select3 area

}
}
}

Data Island regions.php page code

<?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:

With the database in phpcms3.0, I didn't make any changes.

This program can be used directly in the phpcms3.0, if used elsewhere please modify

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.