用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连接mssql的一些相关经验及注意事项

为了能让PHP连接MSSQL,系统需要安装MSSQL,PHP,且在PHP.ini中的配置中,将 ;extension=php_mssql.dll前面的;去掉 1.连接MSSQL 复制代码...

php发送html格式文本邮件的方法

本文实例讲述了php发送html格式文本邮件的方法。分享给大家供大家参考。具体实现方法如下: <?php $to = "simon@mailexample.com,...

php中通过eval实现字符串格式的计算公式

有时候我们对每一种产品都有一个提成公式,而这个计算提成的公式是以字符串格式存在表中的当我们用这个计算公式时,他并不像我们写的:$a=2+3*5;这样简单的能计算出结果,而它是个字符串.所...

PHP使用PHPexcel导入导出数据的方法

本文实例讲述了PHP使用PHPexcel导入导出数据的方法。分享给大家供大家参考,具体如下: 导入数据: <?php error_reporting(E_ALL); /...

PHP输出时间差函数代码

PHP输出时间差函数 复制代码 代码如下:<?php  date_default_timezone_set('PRC'); //默认时区  echo "今天:"...