注意:我假设数据库配置是正确的并且表格因为您没有提供数据库详细信息。
表1:学校
字段类型详细信息
schoolid int(11) 主键
schoolname varchar(50)
detail varchar(50)
表2:类
字段类型详细信息
class_id int(11) 主键
classname varchar(50)
schoolid int(11)
在上面的 db 表中添加示例数据。将数据库配置设置为以下行
$sConn = mysql_connect("localhost","root","sajeda45")
localhost 是主机名 root 是 dbusername sajeda45 是 dbpassword imagedb 是 databasename
在下面的脚本中设置上面的数据库参数,你就会得到你想要的结果
<?php
$sConn = mysql_connect("localhost","root","sajeda45")
or die("Couldn't connect to database server");
$dConn = mysql_select_db("imagedb", $sConn);
function getSchollList()
{
$schoolList = array();
$result = mysql_query("select schoolid,schoolname from school") or die("Couldn't school list");
while($row=mysql_fetch_array($result))
{
$id=$row['schoolid'];
$schoolList["".$id.""] =$row['schoolname'];
}
return $schoolList;
}
function getClassList($schoolId)
{
$classList = array();
$result = mysql_query("select classname from class where schoolid=$schoolId") or die("Couldn't class list");
while($row=mysql_fetch_array($result))
{
$classList[] =$row['classname'];
}
return $classList;
}
?>
<html>
<title>Test Example</title>
<head>
<script type="text/javascript">
function redirectForClass(schoolName)
{
window.location="stackoverflow1.php?school="+schoolName;
}
</script>
</head>
<body>
<?php
$schoolList = getSchollList();
$schoolId = isset($_GET['school']) ? $_GET['school']:0;
$classList = getClassList($schoolId);
?>
<form method="post" action="handler.php">
School:
<select name="school" onchange="redirectForClass(this.value);">
<option value="0">--select school--</option>
<?php
foreach($schoolList as $school_Id => $school){
?>
<option value="<?php echo $school_Id; ?>" <?php if($school_Id==$schoolId) echo "SELECTED"; ?>><?php echo $school; ?></option>
<?php
}
?>
</select>
<br/><br/>
Class:
<select name="class" >
<option value="">--select Class--</option>
<?php
foreach($classList as $class){
?>
<option value="<?php echo $class; ?>"><?php echo $class; ?></option>
<?php
}
?>
</select>
<br/><br/>
<input type="submit" value="submit" />
</form>
</body>
</html>
<?php
?>