php实现三级级联下拉框,
这是我在网上查找到的php实现三级级联下拉框的资料,共享个大家,大家一起进步,具体内容如下
index.php:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Ajax联动菜单</title> <script language="javascript" type="text/javascript" src="aa.js"></script> </head> <body> <form> <label> <select name="sel" id="sel" onChange="showMenu(this.value);"> <option>一级分类</option> <?php $conn = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error()); mysql_select_db('fitment', $conn) or die ('Can\'t use database : ' . mysql_error()); mysql_query("set names 'gb2312'"); $str = "SELECT * FROM `fitment_sort` where supid = '0' ORDER BY `id` ASC"; $result = mysql_query($str) or die("Invalid query: " . mysql_error()); if($result) while ($arr = mysql_fetch_array($result)){ echo <<<EOD <option value={$arr["id"]}>{$arr["name"]}</option> EOD; } mysql_close($conn); ?> </select> </label> <label> <select name="sel2" id="sel2" onChange="showMenu2(this.value)"> <option>二级分类</option> </select> </label> <label> <select name="sel3" id="sel3"> <option>三级分类</option> </select> </label> </form> <input type=button value="send value" onClick="alert(document.getElementByIdx_x_xx_x_xx('sel').options[document.getElementByIdx_x_xx_x_xx('sel').selectedIndex].text + '-' + document.getElementByIdx_x_xx_x_xx('sel2').options[document.getElementByIdx_x_xx_x_xx('sel2').selectedIndex].text + '-' + document.getElementByIdx_x_xx_x_xx('sel3').options[document.getElementByIdx_x_xx_x_xx('sel3').selectedIndex].text)"> </body> </html>
aa.js
<!-- var xmlHttp var xmlHttp2 //函数showMenu(str) - 功能函数1 function showMenu(str) xmlHttp=GetXmlHttpObject() //调用GetXmlHttpObject()函数,创建XMLHttpRequest对象 if (xmlHttp==null) //如果创建失败,则 alert ("Browser does not support HTTP Request") return var url="get2.php" //定义url , 其主要读取数据库 url=url+"?q="+str url=url+"&sid="+Math.random() //Math对象的random方法,取随机数 xmlHttp.onreadystatechange=stateChanged //设置当XMLHttpRequest对象xmlHttp状态改变时调用的函数,注意函数名后不要添加小括号 xmlHttp.open("GET",url,true) //使用XMLHttpRequest对象的open()方法 , 创建HTTP请求 xmlHttp.send(null) //使用XMLHttpRequest对象的open()方法 , 发送HTTP请求 } //函数showMenu(str) - 功能函数1 function showMenu2(str) { xmlHttp2=GetXmlHttpObject() //调用GetXmlHttpObject()函数,创建XMLHttpRequest对象 if (xmlHttp2==null) //如果创建失败,则 alert ("Browser does not support HTTP Request") return var url="get2.php" //定义url , 其主要读取数据库 url=url+"?q2="+str url=url+"&sid="+Math.random() //Math对象的random方法,取随机数 xmlHttp2.onreadystatechange=stateChanged2 //设置当XMLHttpRequest对象xmlHttp状态改变时调用的函数,注意函数名后不要添加小括号 xmlHttp2.open("GET",url,true) //使用XMLHttpRequest对象的open()方法 , 创建HTTP请求 xmlHttp2.send(null) //使用XMLHttpRequest对象的open()方法 , 发送HTTP请求 } //函数BuildSel() , 根据返回的的字串 , 重新构建新的下拉菜单控件Select - 功能函数2 function BuildSel(str,sel) //先清空原来的数据. sel.options.length=0; var arrstr = new Array(); arrstr = str.split(","); if(str.length>0) //循环 for(var i=0;i<arrstr.length;i++) var subarrstr=new Array(); subarrstr=arrstr[i].split("|"); sel.options.add(new Option(subarrstr[1],subarrstr[0])); sel.options[0].selected=true; } //函数stateChanged() - 响应HTTP请求状态变化 function stateChanged() //判断XMLHttpRequest对象的readyState属性值是否为4,如果为4表示异步调用完成(注意:异步调用完成 不代表 异步调用成功) if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") //如果异步调用成功 --> XMLHttpRequest对象的status属性值为200 , html文件在本地运行,则xmlHttp.status的返回值为0,故应该加上xmlHttp.status==0 if(xmlHttp.status==200 || xmlHttp.status==0) //调用文档对象模型DOM的getElementById()方法查找html文件中的标签txtHint , //innerHTML为IE浏览器中的属性,可以用来更改标签间文本的内容 , //xmlHttp.responseText , 通过XMLHttpRequest的responseText属性来获取数据 responseText,结果为字符串;responseXML,结果为XML形式 // document.getElementByIdx_x_xx_x_xx("txtHint").innerHTML=xmlHttp.responseText BuildSel(xmlHttp.responseText,document.getElementsByTagName_r("*").sel2) showMenu2(document.getElementsByTagName_r("*").sel2.value); } } //函数stateChanged() - 响应HTTP请求状态变化 function stateChanged2() //判断XMLHttpRequest对象的readyState属性值是否为4,如果为4表示异步调用完成(注意:异步调用完成 不代表 异步调用成功) if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") //如果异步调用成功 --> XMLHttpRequest对象的status属性值为200 , html文件在本地运行,则xmlHttp.status的返回值为0,故应该加上xmlHttp.status==0 if(xmlHttp2.status==200 || xmlHttp2.status==0) //调用文档对象模型DOM的getElementById()方法查找html文件中的标签txtHint , //innerHTML为IE浏览器中的属性,可以用来更改标签间文本的内容 , //xmlHttp.responseText , 通过XMLHttpRequest的responseText属性来获取数据 responseText,结果为字符串;responseXML,结果为XML形式 // document.getElementByIdx_x_xx_x_xx("txtHint").innerHTML=xmlHttp.responseText BuildSel(xmlHttp2.responseText,document.getElementsByTagName_r("*").sel3) } } //函数GetXmlHttpObject() - 创建XMLHttpRequest对象,即创建一个异步调用对象 function GetXmlHttpObject() var xmlHttp=null; try // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); catch (e) //Internet Explorer try xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); catch (e) xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlHttp; -->
get2.php
<?php header('Content-Type:text/html;charset=GB2312'); $conn = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error()); mysql_select_db('fitment', $conn) or die ('Can\'t use database : ' . mysql_error()); mysql_query("set names 'gb2312'"); //如果传递过来q if($_GET["q"]){ $strs = "SELECT * FROM fitment_sort WHERE supid=' ".$_GET["q"]."' "; //echo $str; $results = mysql_query($strs) or die("Invalid query: " . mysql_error()); if($results) while ($arrs = mysql_fetch_array($results)){ $strings .= $arrs["id"]."|".$arrs["name"].","; } echo substr($strings,0,strlen($strings)-1); } //如果传递过来q2 if($_GET["q2"]){ $str = "SELECT * FROM fitment_sort WHERE supid= '".$_GET["q2"]."'"; //echo $str; $result = mysql_query($str) or die("Invalid query: " . mysql_error()); if($result) while ($arr = mysql_fetch_array($result)){ $string .= $arr["id"]."|".$arr["name"].","; } echo substr($string,0,strlen($string)-1); } mysql_close($conn); ?>
以上就是php实现三级级联下拉框的相关代码,希望对大家学习php程序设计有所帮助。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能