Php+mysql using AJAX technology of provincial and municipal 3-level linkage no Refresh menu source _php Tutorial

Source: Internet
Author: User
It's simple.

1. First put
Add to Header.html page
2. Add the Regionss () function to the/include/tag.php
3. Create a regions.php file in the root directory of your website
4. Add {$REGIONSS (PRC)} to the appropriate place in your template

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


Test page code:



<title>Php+ajax Dynamic Build drop-down menu</title>





Regionss function Code:
//Provincial level 3 linkage drop-down menu function Regionss province. City
Function Regionss ($country = "People's Republic")
{
Global $db; $i = 1;
$text = " state/province \ n"; $result = $db->query ("Select Province from". Table_province. " WHERE country= ' $country ' ORDER by Provinceid '); while ($r = $db->fetch_array ($result)) {$text. = ""; $ i++; } $text. = "";
$text. = " City/county/district \ n ";
$text. = "< Option value= "\"\"" > County-level \ n ";
return $text;

}

Regions.js's Code:

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

}
}
}

Data Island regions.php page code

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:

I'm using a database from phpcms3.0, and I didn't make any changes.

This procedure can be used directly in phpcms3.0, if used in other places, please modify it yourself.

http://www.bkjia.com/PHPjc/631911.html www.bkjia.com true http://www.bkjia.com/PHPjc/631911.html techarticle very simple 1. First put the script Language=javascript src=. /include/js/regions.js/script to header.html page 2. Add the Regionss () function to the/include/tag.php 3. The root of your website ...

  • 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.