用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>

相关文章

Yii 使用intervention/image拓展实现图像处理功能

一:安装intervention/image拓展 composer require intervention/image 二:上传文件 \Intervention\Image\Im...

关于PHP实现异步操作的研究

1.为啥PHP需要异步操作? 一般来说PHP适用的场合是web页面展示等耗时比较短的任务,如果对于比较花时间的操作如resize图片、大数据导入、批量发送EDM、SMS等,就很容易出现...

浅谈PHP中的&lt;&lt;&lt;运算符

PHP中提供了<<<运算符构建多行字符串序列的方法,通常称为here-document或表示为heredoc的简写。 这种方法详细表述了字符串的字面值,并在文本中保留了...

解析php利用正则表达式解决采集内容排版的问题

做采集经常遇到的问题是内容排版问题,用了一些时间写了个用正则替换html标签和样式的函数,共享下。复制代码 代码如下:/** * 格式化内容 * @param str...

PHP单例模式应用示例【多次连接数据库只实例化一次】

本文实例讲述了PHP单例模式应用。分享给大家供大家参考,具体如下: 以前刚开始工作的时候经常连接数据库,每次用到数据库的时候就要用new进行实例并连接一次,当时因为连接数据库的次数不是很...