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!