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:nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <meta> <title>无标题文档</title> <script></script> <script></script> <h1 id="三级联动">三级联动</h1> <p></p>I need to select three drop-down boxes and give them id writing methodsFirst write three drop-down boxes with ids and execute three methods:
$(document).ready(function(e){ //三个下拉列表 //加载显示数据 $("#sanji").html("<select></select><select></select><select></select>"); //加载省份 FillSheng(); //加载市 FillShi(); //加载区 FillQu(); }Next Write the method; The three menus are linked, that is, there are different options according to different provincesDon’t use click() click event here; use it to execute when changing the state The change event change()(1) When the province changes:
//当省份发生变化 $("#sheng").change(function(){ FillShi(); FillQu(); })Urban area, district and county change(2) When the urban area changes :
//当市发生改变 $("#shi").change(function(){ FillQu(); }) });Districts and counties have changed;There is nothing wrong with this logic;The next step is to load the province information roughly, and at the end of the ajax traversal, The value is written into the city's drop-down menu:
//加载省份信息 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>"+data[sj].AreaName+""; } $("#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><option>"+data[sj].AreaName+""; } $("#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><option>"+data[sj].AreaName+""; } $("#qu").html(str); } }); }</option>The format here is JSON. Previously, "TEXT" was used
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; } } }Well, that's rightProcessing page:Finally, the processing page:
<?php $pcode = $_POST["pcode"]; include ("db.class.php"); $db = new db(); $sql = "select * from chinastates where ParentAreaCode = '{$pcode}'"; echo $db->jsonQuery($sql);Connect to the database, call the object class, and return directly to Ouke after writing the sql statement ! ! ! It’s so short! Rendering:
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!

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
