下拉菜单自动获取一二级菜单数据 php+mysql

1.主页面代码:
  <label>主类-></label> 
<select name="bigname" id="bigname" style="font-size: 9pt;border: #999 1px solid;width:150px;height: 20px;line-height: 20px;margin-bottom: 10px;"> 
<?
$sql = "select * from xiangce_fenlei  where id='".$row1['bigclass']."' order by id asc  ";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) > 0) {// 输出小程序数组
while($row = mysql_fetch_array($result)){
?>
  <option value="<?=$row['id']?>">现为:<?=$row['title']?></option> 

 <? }} ?>
 
 
<?
$sql = "select * from xiangce_fenlei  where bigclass='0' order by id asc  ";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) > 0) {// 输出小程序数组
while($row = mysql_fetch_array($result)){
?>
  <option value="<?=$row['id']?>">改正为:<?=$row['title']?></option> 
  <? }} ?>
</select> 
<br />
<label>子类-></label> 
<select name="smallname" id="smallname" style="font-size: 9pt;border: #999 1px solid;width:150px;height: 20px;line-height: 20px;margin-bottom: 10px;"> 
<?
$sql = "select * from xiangce_fenlei  where id='".$row1['class']."' order by id asc  ";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result) > 0) {// 输出小程序数组
while($row = mysql_fetch_array($result)){
?>
  <option value="<?=$row['id']?>">现为:<?=$row['title']?></option> 

 <? }} ?>

</select>
<span style="color:#E33D52;font-size:12px;padding-left:5px;"><br />* 选择《主类》后需等待1-2秒加载《子类》</span>
  &nbsp;
  <script laguage="JavaScript">
function file_name(tt)
{
var win=window.open(tt,"window","location=no")
win.resizeTo(600,250)
win.moveTo(300,500)
}
</script>


<script>
$(document).ready(function(){
 //不需要一开始就重新加载 getSelectVal(); 
  $("#bigname").change(function(){ 
    getSelectVal(); 
console.log("d1")
  }); 
}); 

function getSelectVal(){ 
console.log("d2")
  $.getJSON("api_class.php?bigname="+$("#bigname").val(),function(json){ 
    var smallname = $("#smallname"); 
console.log('f'+$("#bigname").val());
console.log("d3")
    $("option",smallname).remove(); //清空原有的选项 

    $.each(json,function(index,array){ 
      var option = "<option value='"+array['id']+"'>更正为:"+array['title']+"</option>"; 
      smallname.append(option); 
    }); 
  }); 


function newbigclass(){ 
//alert("重新加载中");
console.log("d4");
  $.getJSON("api_bigclass.php",function(json){ 
    var bigname = $("#bigname"); 
console.log("d5"+bigname);
    $("option",bigname).remove(); //清空原有的选项 
    $.each(json,function(index,array){ 
      var option = "<option value='"+array['id']+"'>"+array['title']+"</option>"; 
      bigname.append(option); 
    }); 
  }); 
  
  
    $.getJSON("api_class.php?zt=1",function(json){ 
    var smallname = $("#smallname"); 
console.log("d6"+smallname);
    $("option",smallname).remove(); //清空原有的选项 
    $.each(json,function(index,array){ 
      var option = "<option value='"+array['id']+"'>"+array['title']+"</option>"; 
  console.log("d7"+option);
      smallname.append(option); 
    }); 
  }); 

</script>



2.后台一级分类获取页面

<!---这里显示数据库连接代码-->

  $q=mysql_query("select * from fenlei  where bigclass = 0 and  yh_id='".$_SESSION['yhid']."'  order by id asc       "); 
  while($row=mysql_fetch_array($q)){ 
    $select[] = array("id"=>$row[id],"title"=>$row[title]); 
  } 
  echo json_encode($select); 

3.后台 二级分类获取页面

<!---这里显示数据库连接代码-->

$bigid = $_GET["bigname"]; 

if($bigid=="" and $_GET['zt']==1){
$sql1 ="select * from fenlei  where yh_id='".$_SESSION['yhid']."' order by id asc limit 1   "; 
$result1 = mysql_query($sql1,$conn); //查询
$num_rows1 =mysql_num_rows($result1);//记录总数
if ($num_rows1>0){
while($row1 = mysql_fetch_array($result1)){ 
$bigid=$row1['id'];
}}
}



if(isset($bigid)){ 
  $q=mysql_query("select * from fenlei  where bigclass = $bigid and yh_id='".$_SESSION['yhid']."' order by id asc"); 
  while($row=mysql_fetch_array($q)){ 
    $select[] = array("id"=>$row[id],"title"=>$row[title]); 
  } 
  echo json_encode($select); 


下拉菜单自动获取一二级菜单数据 php+mysql
版权声明:若无特殊注明,本文皆为《 傲世零零 》原创,转载请保留文章出处。
本文链接:下拉菜单自动获取一二级菜单数据 php+mysql http://www.wangjunjiang.com/?post=143
正文到此结束

热门推荐

发表吐槽

你肿么看?

你还可以输入 250 / 250 个字

嘻嘻 大笑 可怜 吃惊 害羞 调皮 鄙视 示爱 大哭 开心 偷笑 嘘 奸笑 委屈 抱抱 愤怒 思考 日了狗

评论信息框

吃奶的力气提交吐槽中...