dtysql0586 2016-03-03 13:54
浏览 29
已采纳

根据下拉列表选项填充列表项元素

I have a course management php page with a database connected in the backend with courses, modules, and modules in a specific course.

I have a form with a dropdown list with options to select a course. Underneath I would like a list of modules in the chosen course from the dropdown list above to show as list item elements. My php code for the form is below. How would I do this using php?

    <form method="post" action="courseManagement.php">
                    <table>
                    <center> <span>Course Management<span></center><P><P><P>
                    <tr>
                    <td>Choose course to add module:     </td>
                         <td> <?php
                            $sql = "SELECT courseName FROM course";
                            $result = mysql_query($sql);
                            echo "<select name='courseName'>";
                                while ($row = mysql_fetch_array($result)) {
                                    echo "<option value='" . $row['courseName'] ."'>" . $row['courseName'] ."</option>";
                                    }
                                    echo "</select>";
                        ?> 
                    </td>

                        </tr>


                        <tr>
                        <td>
                        Modules in the choosen course
                        </td>
                        <td>

                        <li>Projects and management</li>
                        <li>Programming 2</li>
                        <li>Business Analysis</li>

                        </td>
                    </tr>


                    <tr>
                    <td>Choose module to add to course: </td>
                         <td> <?php
                            $sql = "SELECT moduleName FROM module";
                            $result = mysql_query($sql);
                            echo "<select name='moduleName'>";
                                while ($row = mysql_fetch_array($result)) {
                                    echo "<option value='" . $row['moduleName'] ."'>" . $row['moduleName'] ."</option>";
                                    }
                                    echo "</select>";
                        ?> 
                    </td>
                </tr>


                <tr>
                    <td>Choose module to delete from the course: </td>
                         <td> <?php
                            $sql = "SELECT moduleName FROM module";
                            $result = mysql_query($sql);
                            echo "<select name='moduleName'>";
                                while ($row = mysql_fetch_array($result)) {
                                    echo "<option value='" . $row['moduleName'] ."'>" . $row['moduleName'] ."</option>";
                                    }
                                    echo "</select>";
                        ?> 
                    </td>
                </tr>



                </table><p><br>

                <center>    
                <input type="submit" value="Update">
                </center>
                </form> 
  • 写回答

1条回答 默认 最新

  • dongyan4157 2016-03-03 14:06
    关注

    If you can call ajax it's very simple to use this code:

    SELECT * FROM course;
    

    Your dropdown code is something like this:

    <select name="course" id="course">
       <option value='1'>English</option>
       <option value='2'>Gujarati</option>
    </select>
    

    Then you want to selectable course by module display in second dropdown, so you can use this in jQuery:

    $(document).ready(function() { 
    $('#course').change(function(){
            var courseid = $(this).val();
            if(courseid != '' && $.isNumeric(courseid)){
                $.post('path of file',{id:courseid}, function(result){
                    // usable code //
                }); 
            }
        });
    });
    

    Then this query write in which file are you call.

    id = $this->input->post('id');
    

    SQL:

     SELECT module.*,course.* 
     FROM course 
     LEFT JOIN module ON course.moduleId = module.Id 
     WHERE course.moduleId = id;
    

    You can call this query using ajax call, and you get all list of module by courseid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?