Ajax three-level linkage drop-down menu implementation (with code)
This time I will bring you the three-level linkage of ajax Drop-down menu Implementation (with code), what are the precautions to implement the ajax three-level linkage drop-down menu, the following is the actual combat Let’s take a look at the case.
To write three-level linkage with ajax, write a file class first, and then call it directly when you use it later;
Find a table:
Realization:
Three-level linkage in China: province, city, district;
Picture:
## Let’s talk about the idea:
(1) When the user selects a province, the event is triggered and the current province id is passed through ajax The request is sent to the program on the server
(2) For example, if the Chinese region is taken, China is 0001, then the one with the number 0001 is the Chinese region;
The code name of Beijing is 11, so the sub-code number 11 is the urban area of Beijing. That is to say, the sub-code number is queried based on the main code number;(3 ) The server queries the database according to the client's request, and returns it to the client in a certain format
The display page is very simple, it only requires a p, and introduces js and jquery files:<!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" /> <title>无标题文档</title> <script src="jquery-1.11.2.min.js"></script> <script src="sanji.js"></script> </head> <body> <h1>三级联动</h1> <p id="sanji"></p> </body> </html>
$(document).ready(function(e){ //三个下拉列表 //加载显示数据 $("#sanji").html("<select id='sheng'></select><select id='shi'></select><select id='qu'></select>"); //加载省份 FillSheng(); //加载市 FillShi(); //加载区 FillQu(); }
//当省份发生变化 $("#sheng").change(function(){ FillShi(); FillQu(); })
//当市发生改变 $("#shi").change(function(){ FillQu(); }) });
//加载省份信息 function FillSheng() { //根据父级代号 //取父级代号 var pcode = "0001"; //根据父级代号查数据 $.ajax({ async:false, url:"cl.php", data:{pcode:pcode}, type:"POST", dataType:"JSON", success:function(data) { var str = ""; for(var sj in data) { str = str+"<option value = '"+data[sj].AreaCode+"'>"+data[sj].AreaName+"</optiom>"; } $("#sheng").html(str); } }); } //加载市 function FillShi() { //根据父级代号 //取父级代号 var pcode = $("#sheng").val(); //根据父级代号查数据 $.ajax({ async:false, //取消异步 url:"cl.php", data:{pcode:pcode}, type:"POST", dataType:"JSON", success:function(data) { var str = ""; for(var sj in data) { str = str+"<option value = '"+data[sj].AreaCode+"'>"+data[sj].AreaName+"</optiom>"; } $("#shi").html(str); } }); } //加载区 function FillQu() { //根据父级代号 //取父级代号 var pcode = $("#shi").val(); //根据父级代号查数据 $.ajax({ url:"cl.php", data:{pcode:pcode}, type:"POST", dataType:"JSON", success:function(data) { var str = ""; for(var sj in data) { str = str+"<option value = '"+data[sj].AreaCode+"'>"+data[sj].AreaName+"</optiom>"; } $("#qu").html(str); } }); }
Note: JSON
JSON is a syntax for passing objects. The objects can be name/value pairs, arrays and other objectsWe are using arrays, then we need toTraverse the array, to get each piece of data, the method used to traverse the array in js is
for(var sj in data)
{
}
to traverse the array. Format! ! ! Let’s write the file encapsulation class mentioned above, and find our previousConnect database class:
Add this paragraph:public function jsonQuery($sql,$type=1) { $db = new mysqli($this->host,$this->zhang,$this->mi,$this->dbname); $r = $db->query($sql); if($type == "1") { $arr = $r->fetch_all(MYSQLI_ASSOC); return json_encode($arr); //去掉最后竖线 } else { return $r; } } }
<?php $pcode = $_POST["pcode"]; include ("db.class.php"); $db = new db(); $sql = "select * from chinastates where ParentAreaCode = '{$pcode}'"; echo $db->jsonQuery($sql);
How to use fileinput to implement asynchronous ajax upload
Ajax detailed steps to implement database modification and addition functions
The above is the detailed content of Ajax three-level linkage drop-down menu implementation (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to make the WPS table drop-down menu: After selecting the cell where you want to set the drop-down menu, click "Data", "Validity" in sequence, and then make the corresponding settings in the pop-up dialog box to pull down our menu. As a powerful office software, WPS has the ability to edit documents, statistical data tables, etc., which provides a lot of convenience for many people who need to deal with text, data, etc. In order to skillfully use WPS software to provide us with a lot of convenience, we need to be able to master various very basic operations of WPS software. In this article, the editor will share with you how to use WPS software. Perform drop-down menu operations in the WPS table that appears. After opening the WPS form, first select the

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make
