Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ThinkPHP+Ajax to realize two-stage pull-down menu

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use ThinkPHP+Ajax to achieve two-level linkage drop-down menu, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The first is the design of the database. The classification table is called cate.

What I do is the secondary linkage of classified data, the fields needed for the data are: id,name (Chinese name), pid (parent id).

The setting of the parent id: if the data does not have an upper level, the parent id is 0; if there is a superior, the parent id is the id of the previous level.

After the database has content, you can start to write code for the realization of two-level linkage.

First, get all the data with a pid of 0 in the background php, save it to $cate, and then loop the output with foreach in the first layer.

Html Code:

Please select a type

{$v.ca_name}

Label:

Ajax Code:

$('# type') .click (function () {

$(this) .change (function () {

Var objectModel = {}

Var value = $(this) .val ()

Var type = $(this) .attr ('id')

ObjectModel [type] = value

$.ajax ({

Cache:false

Type: "POST"

Url:site.web+ "lable"

DataType: "json"

Data:objectModel

Timeout:30000

Error:function () {

Alert (site.web+ "lable")

}

Success:function (data) {

("# lables") .empty ()

Var count = data.length

Var I = 0

Var b = ""

For (array ('ca_pid'= > $cate))-> field (' ca_id,ca_name')-> select ()

$this- > ajaxReturn ($result, "JSON")

The I () method of Thinkphp can actually be seen as $_ POST [] to get the id of the first layer selection passed by ajax, then get its subclass and return it to ajax with ajaxReturn (). Here, the returned data is set to be in the form of json, so ajax will receive the data in the form of json.

The way in which native php returns data:

/ / the search result is $result

.

Echo json_encode ($result)

This completes the implementation of the two-level linkage drop-down menu, it should be noted that the url must be correct, and the background also needs to have a return value, otherwise ajax will not execute the success method.

The above is all the contents of the article "how to use ThinkPHP+Ajax to achieve 2-level linkage drop-down menu". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report