您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)使用php怎么編寫一個三級級聯(lián)下拉框功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
index.php:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Ajax聯(lián)動菜單</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 //函數(shù)showMenu(str) - 功能函數(shù)1 function showMenu(str) xmlHttp=GetXmlHttpObject() //調(diào)用GetXmlHttpObject()函數(shù),創(chuàng)建XMLHttpRequest對象 if (xmlHttp==null) //如果創(chuàng)建失敗,則 alert ("Browser does not support HTTP Request") return var url="get2.php" //定義url , 其主要讀取數(shù)據(jù)庫 url=url+"?q="+str url=url+"&sid="+Math.random() //Math對象的random方法,取隨機(jī)數(shù) xmlHttp.onreadystatechange=stateChanged //設(shè)置當(dāng)XMLHttpRequest對象xmlHttp狀態(tài)改變時調(diào)用的函數(shù),注意函數(shù)名后不要添加小括號 xmlHttp.open("GET",url,true) //使用XMLHttpRequest對象的open()方法 , 創(chuàng)建HTTP請求 xmlHttp.send(null) //使用XMLHttpRequest對象的open()方法 , 發(fā)送HTTP請求 } //函數(shù)showMenu(str) - 功能函數(shù)1 function showMenu2(str) { xmlHttp2=GetXmlHttpObject() //調(diào)用GetXmlHttpObject()函數(shù),創(chuàng)建XMLHttpRequest對象 if (xmlHttp2==null) //如果創(chuàng)建失敗,則 alert ("Browser does not support HTTP Request") return var url="get2.php" //定義url , 其主要讀取數(shù)據(jù)庫 url=url+"?q2="+str url=url+"&sid="+Math.random() //Math對象的random方法,取隨機(jī)數(shù) xmlHttp2.onreadystatechange=stateChanged2 //設(shè)置當(dāng)XMLHttpRequest對象xmlHttp狀態(tài)改變時調(diào)用的函數(shù),注意函數(shù)名后不要添加小括號 xmlHttp2.open("GET",url,true) //使用XMLHttpRequest對象的open()方法 , 創(chuàng)建HTTP請求 xmlHttp2.send(null) //使用XMLHttpRequest對象的open()方法 , 發(fā)送HTTP請求 } //函數(shù)BuildSel() , 根據(jù)返回的的字串 , 重新構(gòu)建新的下拉菜單控件Select - 功能函數(shù)2 function BuildSel(str,sel) //先清空原來的數(shù)據(jù). sel.options.length=0; var arrstr = new Array(); arrstr = str.split(","); if(str.length>0) //循環(huán) 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; } //函數(shù)stateChanged() - 響應(yīng)HTTP請求狀態(tài)變化 function stateChanged() //判斷XMLHttpRequest對象的readyState屬性值是否為4,如果為4表示異步調(diào)用完成(注意:異步調(diào)用完成 不代表 異步調(diào)用成功) if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") //如果異步調(diào)用成功 --> XMLHttpRequest對象的status屬性值為200 , html文件在本地運行,則xmlHttp.status的返回值為0,故應(yīng)該加上xmlHttp.status==0 if(xmlHttp.status==200 || xmlHttp.status==0) //調(diào)用文檔對象模型DOM的getElementById()方法查找html文件中的標(biāo)簽txtHint , //innerHTML為IE瀏覽器中的屬性,可以用來更改標(biāo)簽間文本的內(nèi)容 , //xmlHttp.responseText , 通過XMLHttpRequest的responseText屬性來獲取數(shù)據(jù) responseText,結(jié)果為字符串;responseXML,結(jié)果為XML形式 // document.getElementByIdx_x_xx_x_xx("txtHint").innerHTML=xmlHttp.responseText BuildSel(xmlHttp.responseText,document.getElementsByTagName_r("*").sel2) showMenu2(document.getElementsByTagName_r("*").sel2.value); } } //函數(shù)stateChanged() - 響應(yīng)HTTP請求狀態(tài)變化 function stateChanged2() //判斷XMLHttpRequest對象的readyState屬性值是否為4,如果為4表示異步調(diào)用完成(注意:異步調(diào)用完成 不代表 異步調(diào)用成功) if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") //如果異步調(diào)用成功 --> XMLHttpRequest對象的status屬性值為200 , html文件在本地運行,則xmlHttp.status的返回值為0,故應(yīng)該加上xmlHttp.status==0 if(xmlHttp2.status==200 || xmlHttp2.status==0) //調(diào)用文檔對象模型DOM的getElementById()方法查找html文件中的標(biāo)簽txtHint , //innerHTML為IE瀏覽器中的屬性,可以用來更改標(biāo)簽間文本的內(nèi)容 , //xmlHttp.responseText , 通過XMLHttpRequest的responseText屬性來獲取數(shù)據(jù) responseText,結(jié)果為字符串;responseXML,結(jié)果為XML形式 // document.getElementByIdx_x_xx_x_xx("txtHint").innerHTML=xmlHttp.responseText BuildSel(xmlHttp2.responseText,document.getElementsByTagName_r("*").sel3) } } //函數(shù)GetXmlHttpObject() - 創(chuàng)建XMLHttpRequest對象,即創(chuàng)建一個異步調(diào)用對象 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); ?>
關(guān)于使用php怎么編寫一個三級級聯(lián)下拉框功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。