標籤:cal pre selected lis localhost onchange alt 串連 end
<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><?phpendwhile;?></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簡單實現二級聯動