用php+javascript实现二级级联菜单的制作

yipeiwu_com6年前PHP代码库
 

大体思路是这样的:为了不让先前的页面刷新,我用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(!empty($_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>

相关文章

php短址转换实现方法

php短址转换实现方法

本文实例讲述了php短址转换实现方法。分享给大家供大家参考。具体分析如下: 从昨天下午到现在 推翻了一个又一个的信息存储方案,从mysql到加上内存不受限制的file_get_conte...

PHP7内核CGI与FastCGI详解

PHP7内核CGI与FastCGI详解

CGI:是 Web Server 与 Web Application 之间数据交换的一种协议。 FastCGI:同 CGI,是一种通信协议,但比 CGI 在效率上做了一些优化。 PHP-...

Windows下的PHP安装文件线程安全和非线程安全的区别

从2000年10月20日发布的第一个Windows版的PHP3.0.17开始的都是线程安全的版本,这是由于与Linux/Unix系统是采用 多进程的工作方式不同的是Windows系统是采...

php的命名空间与自动加载实现方法

类的自动加载 引子 当我们在php代码中加载类时,我们必须要include或者require 某个类文件。 但遇到类似的情况,例如: require "Class1.php";...

php使用gettimeofday函数返回当前时间并存放在关联数组里

本文实例讲述了php使用gettimeofday函数返回当前时间并存放在关联数组里的方法。分享给大家供大家参考。具体分析如下: 英文官方描述如下: Key Descriptio...