Web Front-end
JS Tutorial
jQuery PHP MySQL secondary linkage drop-down menu example explanation_jquery
jQuery PHP MySQL secondary linkage drop-down menu example explanation_jquery
Second-level linkage drop-down menu selection is used in many places, such as province and city drop-down linkage, and product size drop-down selection linkage. This article will explain through examples how to use jQuery PHP MySQL to achieve the secondary drop-down linkage effect of size classification.
Implementation effect: When a major category is selected, the options in the sub-category drop-down box also change.
Implementation principle: According to the value of the major category, the value is passed to the background PHP for processing through jQuery. PHP queries the MySQl database to obtain the corresponding small category and returns JSON data For front-end processing.
XHTML
First we need to create two drop-down selection boxes, the first is a major category and the second is a small category. The values of major categories can be written in advance or read from the database.
<label>大类:</label> <select name="bigname" id="bigname"> <option value="1">前端技术</option> <option value="2">程序开发</option> <option value="3">数据库</option> </select> <label>小类:</label> <select name="smallname" id="smallname"> <option value="1">flash</option> <option value="2">ps</option> </select>
jQuery
First write a function to get the value of the large category selection box, pass it to the backend server.php through the $.getJSON method, read the JSON data returned by the backend, and traverse the JSON data through the $.each method to write the corresponding value. An option string, and finally append the option to the subclass.
function getSelectVal(){
$.getJSON("server.php",{bigname:$("#bigname").val()},function(json){
var smallname = $("#smallname");
$("option",smallname).remove(); //清空原有的选项
$.each(json,function(index,array){
var option = "<option value='"+array['id']+"'>"+array['title']+"</option>";
smallname.append(option);
});
});
}
Note that before traversing and appending JSON data, you must first clear the original items in the subcategory. There are two ways to clear options, one is mentioned in the code above, and there is a simpler and more direct method:
smallname.();
Then, execute the calling function after the page loads:
$(function(){
getSelectVal();
$("#bigname").change(function(){
getSelectVal();
});
});
When the page is initialized, the drop-down box needs to set the options, so getSelectVal() is called at the initial stage, and when the major category options change, getSelectVal() is also called.
PHP
include_once("connect.php"); //链接数据库
$bigid = $_GET["bigname"];
if(isset($bigid)){
$q=mysql_query("select * from catalog where cid = $bigid");
while($row=mysql_fetch_array($q)){
$select[] = array("id"=>$row[id],"title"=>$row[title]);
}
echo json_encode($select);
}
Construct an SQL statement to query the classification table based on the value of the major category passed by jQuery, and finally output JSON data. Without special instructions, the PHP and MySQL connections and query statements used by this site all use original statement methods such as mysql_query, etc. The purpose is to allow readers to intuitively know the data transmission query.
Finally, attach the MYSQL table structure:
CREATE TABLE `catalog` ( `id` mediumint(6) NOT NULL auto_increment, `cid` mediumint(6) NOT NULL default '0', `title` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
The above is an introduction to how to implement a secondary linkage drop-down menu by combining jQuery, PHP, and MySQL. The program still has some shortcomings and needs to be improved. I hope this article can give you some inspiration.
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
1378
52
How do I create and publish my own JavaScript libraries?
Mar 18, 2025 pm 03:12 PM
Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.
How do I optimize JavaScript code for performance in the browser?
Mar 18, 2025 pm 03:14 PM
The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.
What should I do if I encounter garbled code printing for front-end thermal paper receipts?
Apr 04, 2025 pm 02:42 PM
Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...
How do I debug JavaScript code effectively using browser developer tools?
Mar 18, 2025 pm 03:16 PM
The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.
Who gets paid more Python or JavaScript?
Apr 04, 2025 am 12:09 AM
There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.
How do I use source maps to debug minified JavaScript code?
Mar 18, 2025 pm 03:17 PM
The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.
Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts
Mar 15, 2025 am 09:19 AM
This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion
The difference in console.log output result: Why are the two calls different?
Apr 04, 2025 pm 05:12 PM
In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...


