用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+RabbitMQ实现消息队列的完整代码

前言 为什么使用RabbitMq而不是ActiveMq或者RocketMq? 首先,从业务上来讲,我并不要求消息的100%接受率,并且,我需要结合php开发,RabbitMq相较Rock...

PHP中余数、取余的妙用

<?php $ary=array("name","egineer","sonny","tonny","pingk","apple","phone","clone",...

探讨php define()函数及defined()函数使用详解

The define() function defines a constant.define()函数的作用是:定义一个常量。 Constants are much like varia...

基于php下载文件的详解

php下载文件,比如txt文件。出现的效果就是,弹出浏览器自带的下载框,出现另存为操作。有时候会出现内存溢出和超时的现象。超时的话,设置set_time_limit(0);出现内存溢出的...

详解WordPress开发中wp_title()函数的用法

wp_title 函数在 WordPress 中是用来显示文章、页面、分类等等等等标题的一个函数,但在首页索引,该函数将不显示任何的东西。该函数在 WordPress 官方主题中一直被使...