大體思路是這樣的:為了不讓先前的頁面刷新,我用iframe潛入了一個二級子頁面,用來讀取數據庫中的數據,最后把想要的數據傳遞給父級頁面,完成數據的選擇和轉移。
主要程序代碼如下(部分代碼有改動,但不影響功能):
父頁面reg.html:
- <iframe src=”city.php” width=”300″ height=”22″ frameborder=”0″ scrolling=”no”></iframe> <input name=”city” type=”hidden” id=”city” value=”" />
子頁面city.php:
- <script language=”javascript” type=”text/javascript”>
- function goto(n){
- this.location.href=”city.php?sh_id=”+n;
- }
- </script>
- <select name=”sh” onchange=”goto(this.value)”>
- <option>請選擇所在省市</option>
- <?php
- include_once(”db.php”);
- $sql=”select * from province order by sh_id asc”;
- $result=mysql_query($sql);
- while($row=mysql_fetch_assoc($result)){
- ?>
- <option value=”<? echo $row[”sh_id”];?>” <? if($_GET[”sh_id”]==$row[”sh_id”]){echo 'selected=”selected”‘;}?>><? echo $row[”sh_name”];?></option>
- <?php
- }
- ?>
- </select>
- <select name=”city” onchange=”parent.document.getElementById('city').value=this.value”>
- <option>選擇你所在的城市</option>
- <?php
- if(!emptyempty($_GET[”sh_id”])){
- //echo “ok”;
- $sql=”select * from city where sh_id=”.$_GET[”sh_id”].” order by city_id asc”;
- $result=mysql_query($sql);
- while($row=mysql_fetch_assoc($result)){
- ?>
- <option value=”<? echo $row[”city_name”];?>”><? echo $row[”city_name”];?></option>
- <?php
- }
- }
- ?>
- </select>