<script type="text/javascript">
//当第一级选项发生变化的时候跳转
function jump() {
location.href='?firstid='+document.getElementById('first').value;
}

</script>
<?php
//连接数据库
@mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');

$firstid=isset($_GET['firstid'])?$_GET['firstid']:'';	//获取提交的firstid
?>
<!--获取第一级-->
<select id='first' onchange='jump()'>
<option value="">---请选择---</option>
<?php
$sql='select * from shoplist where parentid=0';
$rs=mysql_query($sql);
while($rows=mysql_fetch_assoc($rs)):
?>
<option value="<?php echo $rows['id']?>" <?php echo $rows['id']==$firstid?'selected':''?>><?php echo $rows['name']?></option>
<?php
endwhile;
?>
</select>
<!--获取第二级-->
<select id='second'>
<option value="">---请选择---</option>
<?php
$sql="select * from shoplist where parentid=$firstid";
$rs=mysql_query($sql);
while($rows=mysql_fetch_assoc($rs)): 
?>
<option value="<?php echo $rows['id']?>"><?php echo $rows['name']?></option>
<?php endwhile;	?>
</select>

 结果

php简单实现二级联动

 

相关文章:

  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-11-29
  • 2021-12-22
  • 2022-12-23
猜你喜欢
  • 2021-06-30
  • 2022-12-23
  • 2021-12-16
  • 2019-03-21
  • 2022-12-23
  • 2021-12-26
相关资源
相似解决方案