用php實現真正的連動下拉式清單

來源:互聯網
上載者:User

下面的兩個檔案copy到同一個檔案下,通過web路徑訪問index.html看看吧,這個是通過iframe來實現的連動更新,list.php目前的內容比較簡單,你看通過list.php來進行資料庫查詢,然後顯示出查詢的結果清單。

index.html

<body>
<form name="myfrm">
<select name="mlist" onchange="changes();">
<option value="0">請選擇...</option>
<option value="北京">北京</option>
<option value="通化">通化</option>
</select>&nbsp;
<select name="slist">
</select>
<iframe id="frame" src="list.php?city=" style="display:none;"></iframe>
<script language="javascript">
function changes(){
frame.location.href = "list.php?city=" + document.myfrm.mlist.value;
}
</script>
</form>
</body>

list.php

<?php
$data = array("北京"=>array("小強","旺財","小強他爹"),
"通化"=>array("小溫","小宋","他們兒子"),);
$city = $_get["city"];
$result = $data[$city];
$str = "<script language=\"javascript\">list = parent.document.myfrm.slist;list.length = 0;";
if($result==null)
$str .= "tmp = new option(\"......\", \"\");list.options[0] = tmp;";
else
foreach($result as $i => $value)
$str .= "tmp = new option(\"{$value}\", \"{$value}\");list.options[$i] = tmp;";
$str .= "</script>";
echo $str;
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.