1

I need help regarding one combobox dependent on another combobox.

I have 2 combo fields in my form, one is of schools and other is of class. Every school has specific class and both are stored in databases. I have to pick the value of school from database (school table) and after selecting it 2nd combo box will show the classes related to the specific school.

After selecting these values I have to store them in database too.

4

2 回答 2

0

注意:我假设数据库配置是正确的并且表格因为您没有提供数据库详细信息。

表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

?>
于 2012-05-02T13:06:51.700 回答
0

有两种方法可以解决这个问题。

  1. 使用 javascript:

    1. 您可以使用 Javascript + Ajax 动态获取第二个框的数据源,创建一个数组并设置组合框的项目。
    2. 您可以在数组中加载所需的选项(创建页面时使用 php)

    在这两种方式中,第一个组合框中的每个选项都有一个数组(两个维度数组 - 第一个索引用于第一个组合框中的选择,第二个是将添加到第二个组合框的项目的索引)。然后你使用

    document.getElementById("DropDownList").options.add(opt);

当第一个的值发生变化时,在第二个组合框中添加项目。

2. 每次更改第一个组合框的值时,您可以为表单调用 submit(),然后使用 php 填充第二个组合框(取决于第一个组合框的值)。

于 2012-05-02T10:17:26.680 回答