How to implement Ajax's three-level linkage product classification
This time I will show you how to implement Ajax's three-level linkage product classification, and what are the precautions for implementing Ajax's three-level linkage product classification. The following is a practical case, let's take a look.
Idea analysis:
Effect: When the page is loaded, ajax is used to asynchronously request data from the background, load the first-level product category, and when the page is selected When selecting first-level products, load second-level products, and select second-level products to load third-level products.
Implementation:
1. After getting the data, load the product with pid 0, and dynamically create an option to add the product to the first level. menu, and set the value
2. When selecting a first-level product, load the product with pid=current id, and create an option to append the product to the secondary menu, and set the value Value
3. When selecting a secondary product, load the product with pid = current id, create an option to append the product to the third-level menu, and set the value
Page effect:
$(function(){ //请求路径 var url="03goods.php"; //option默认内容 var option="<option value='0'>未选择</option>"; //获取jq对象 var $sel1=$(".sel1"); var $sel2=$(".sel2"); var $sel3=$(".sel3"); //自动生成一个<option>元素 function createOption(value,text){ var $option=$("<option></option>"); $option.attr("value",value); $option.text(text); return $option; } //加载数据 function ajaxSelect($select,id){ //get请求 $.get(url,{"pid":id},function(data){ $select.html(option); for(var k in data ){ $select.append(createOption(data[k].id,data[k].name)); } },"json"); } //自动加载第一个下拉菜单 ajaxSelect($sel1,"0"); //选择第一个下拉菜单时加载第二个 $sel1.change(function(){ var id=$sel1.val(); if(id=="0"){ $sel2.html(option); $sel3.html(option); }else{ ajaxSelect($sel2,id); } }); //选择第二个下拉菜单时加载第三个 $sel2.change(function(){ var $id=$sel2.val(); if($id=="0"){ $sel3.html(option); }else{ ajaxSelect($sel3,$id); } }); });
Backend code:
<?php header('Content-Type:text/html; charset=utf-8'); //数据 $arr=array( //array(分类id,分类名,分类的父id) array('id'=>'1','name'=>'数码产品','pid'=>'0'), array('id'=>'2','name'=>'家电','pid'=>'0'), array('id'=>'3','name'=>'书籍','pid'=>'0'), array('id'=>'4','name'=>'服装','pid'=>'0'), array('id'=>'5','name'=>'手机','pid'=>'1'), array('id'=>'6','name'=>'笔记本','pid'=>'1'), array('id'=>'7','name'=>'平板电脑','pid'=>'1'), array('id'=>'8','name'=>'智能手机','pid'=>'5'), array('id'=>'9','name'=>'功能机','pid'=>'5'), array('id'=>'10','name'=>'电视机','pid'=>'2'), array('id'=>'11','name'=>'电冰箱','pid'=>'2'), array('id'=>'12','name'=>'智能电视','pid'=>'10'), array('id'=>'13','name'=>'编程书籍','pid'=>'3'), array('id'=>'14','name'=>'JavaScript','pid'=>'13'), ); //获取指定分类的商品 function getByPid($arr,$pid){ $result=array(); foreach($arr as $v){ if($v['pid']==$pid){ $result[]=$v; } } return $result; } //获取请求参数 $pid=isset($_GET['pid'])?$_GET['pid']:'0'; $result=getByPid($arr,$pid); //输出json数据 echo json_encode($result); ?>
I believe you have mastered the method after reading the case in this article, more exciting Please pay attention to other related articles on php Chinese website!
Recommended reading:
How to achieve secondary linkage with Ajax combined with PHP
The components and core of Ajax technology that novices must read Principle analysis
The above is the detailed content of How to implement Ajax's three-level linkage product classification. 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



Journey through the vastness and set foot on the journey to the west! Today, Zhengtu IP officially announced that it will launch a cross-border cooperation with CCTV animation "Journey to the West" to jointly create a cultural feast that combines tradition and innovation! This cooperation not only marks the in-depth cooperation between the two major domestic classic brands, but also demonstrates the unremitting efforts and persistence of the Zhengtu series on the road of promoting Chinese traditional culture. Since its birth, the Zhengtu series has been loved by players for its profound cultural heritage and diversified gameplay. In terms of cultural inheritance, the Zhengtu series has always maintained respect and love for traditional Chinese culture, and skillfully integrated traditional cultural elements into the game, bringing more fun and inspiration to players. The CCTV animation "Journey to the West" is a classic that has accompanied the growth of generations.

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

NetEase's "Onmyoji" mobile game announced today that the Onmyoji x Hatsune Miku limited collaboration will officially begin on March 6. The collaboration-limited SSR Hatsune Miku (CV: Saki Fujita) and SSR Kagamine Rin (CV: Asami Shimoda) are coming to Heian Kyo! The linkage online special performance event will officially start in the game on March 9~

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

On the date, "Backwater Cold" officially announced that it will launch a linkage with KFC from April 19th to May 12th. However, the specific content of the linkage has left many people stunned. They repeatedly said, "It's embarrassing to heaven" and "It's important to society." died"! The reason lies in the slogan of this theme event. Friends who have seen the KFC linkage of "Genshin Impact" and "Beng Tie" must have the impression that "encountering another world and enjoying delicious food" has become a reality in "Ni Shui Han" Now: shout out to the clerk, "God is investigating the case, who are you?" The clerk needs to reply, "Fried chicken is a big business, and there is no room for error!" Training guide for employees: Never laugh! Not only that, this collaboration also held a dance competition. If you go to the theme store and perform the "Dance when you hear 'Ji'" dance move, you can also get a small rocking music stand. Embarrassing, so embarrassing! But that's what I want

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.
