Home php教程 php手册 Ajax+php无限联动下拉菜单实例

Ajax+php无限联动下拉菜单实例

May 25, 2016 pm 04:55 PM
foreach select

Ajax无限联动实现方法就是先由用户选择大类,然后我们再用ajax实现数据局部请求给php页面处理,php把用户需要的数据返回到html js进行处理,这样就实现了无限联动下拉菜单的效果了。

首先是 Ajax.php文件:

这个文件我觉着就是接收数据处理数据的

 代码如下 复制代码

mysql_connect("localhost","root","");
mysql_select_db("aaa");
mysql_query("set names 'UTF8'");

上面的这些代码 不用我说都知道是连接数据库的

 代码如下 复制代码

//select 语句
1.//$q=mysql_query("select * from `newstype` where `kid`='".$_POST['id']."'");
2.$sql="select * from `newstype` where `kid`='".$_POST['id']."'";

$q=mysql_query($sql);

上面的1和2的选项是因为 我写了一遍select语句出现报错了 然后我就又写了一遍 结果两个都对了 1 是注释掉了

if(mysql_num_rows($q)!=0){  判断查找的语句的个数 如果是0的话就代表下面没有分支了 就不会显示了

//记住在$_POST[]加()这是我出现的错误

 代码如下 复制代码
echo "

输出一个select选择框会添加到后来最终显示的页面的div里面 后面会做介绍

 代码如下 复制代码

while($rs=mysql_fetch_array($q)){
echo"";select里面的option选项 value的值一定要给 因为我们要按照这个查找语句
}
echo "";
echo "

";因为做的是无限极联动 后面还要输出一个div的框 用来盛下一个的select框
}
?>


--------------------------------------------------------------------------------

下面的是Ajax.js文件var xmlhttp;定义一个变量

 代码如下 复制代码

function createxml(){这个部分主要是用来判断浏览器
if(window.XMLHttpRequest){ XMLHttpRequest是javascript里面内置的属性 具体的作用 大家自己去翻手册
xmlhttp=new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
}
function fun(n){  这个方法是展示页中 select onchange事件调用
createxml();
var id=document.getElementById("s"+n).value;  是选取对应的id的select里面的value值 就是我们上面说道的数据库中的ID字段
xmlhttp.open("post","Ajax.php",true);  打开请求
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//id之后还有一个+
xmlhttp.send("id="+id+"&num="+n); 发送请求 获取这个id的值 和 n的值 并且分别赋值给 id 和 num
xmlhttp.onreadystatechange=function(){stylefun(n)}; 触发onreadystatechange调用stylefun(n)方法 并且设置n参数
}
function stylefun(n){
if(xmlhttp.readyState==4 && xmlhttp.status==200){已经接受完成之后 把获得的text赋值给对应id的div标签里面
//innerHTML 记住html是大写
document.getElementById("list"+n).innerHTML=xmlhttp.responseText;
}
}


--------------------------------------------------------------------------------

下面是展示页面liandong.php(原谅我起名字的时候都是很简单的思维)

 代码如下 复制代码

mysql_connect("localhost","root","")or die("链接数据库失败");
mysql_select_db("aaa");
mysql_query("set names'UTF8'");
$sql="select * from `newstype` where `kid` = 0";先查找最初的根目录类型
$query=mysql_query($sql);
while($rs=mysql_fetch_array($query)){
$arr[]=$rs;
}

?>














文章网址:

随意转载^^但请附上教程地址。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

Asynchronous processing method of Select Channels Go concurrent programming using golang Asynchronous processing method of Select Channels Go concurrent programming using golang Sep 28, 2023 pm 05:27 PM

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to hide the select element in jquery How to hide the select element in jquery Aug 15, 2023 pm 01:56 PM

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

What is the reason why Linux uses select? What is the reason why Linux uses select? May 19, 2023 pm 03:07 PM

Because select allows developers to wait for multiple file buffers at the same time, it can reduce IO waiting time and improve the IO efficiency of the process. The select() function is an IO multiplexing function that allows the program to monitor multiple file descriptors and wait for one or more of the monitored file descriptors to become "ready"; the so-called "ready" state is Refers to: the file descriptor is no longer blocked and can be used for certain types of IO operations, including readable, writable, and exceptions. select is a computer function located in the header file #include. This function is used to monitor file descriptor changes—reading, writing, or exceptions. 1. Introduction to the select function. The select function is an IO multiplexing function.

PHP returns the current element in an array PHP returns the current element in an array Mar 21, 2024 pm 12:36 PM

This article will explain in detail about the current element in the array returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Get the current element in a PHP array PHP provides a variety of methods for accessing and manipulating arrays, including getting the current element in an array. The following introduces several commonly used techniques: 1. current() function The current() function returns the element currently pointed to by the internal pointer of the array. The pointer initially points to the first element of the array. Use the following syntax: $currentElement=current($array);2.key() function key() function returns the array internal pointer currently pointing to the element

See all articles