dongshadu4498 2014-12-11 10:36
浏览 47
已采纳

在选择组合框值时,必须在从数据库中选择查询后动态显示标签

In a file following code is written which populate the value of combobox :

<select id="company" required="required" class="form-control" value = "select"    name="subject_code" placeholder="Select" >
   <?php
     //Iterating list of subjects available to be filled .
    echo "<option> Select </option>";
     foreach ($subjects_to_fill as $subject_id => $subject_name) {
          # code...                             
             echo "<option value=".$subject_id."> ".$subject_name." </option>";

      }                                              
     ?>
</select> 

On selecting a particular item from the combobox, I want to display the faculty_name dynamically from faculty_table on the basis of $subject_id.

table structure:

faculty_table(faculty_id,faculty_name,subject_id)
subject_table(subject_id,subject_name,faculty_id)
  • 写回答

1条回答 默认 最新

  • 普通网友 2014-12-11 11:01
    关注

    You will need to use ajax for this task.

    Add a <div> and below script in your current file.

    <div id="faculty"></div>
    <script type="text/javascript">
         function get_faculty()
           {
            var id = document.getElementById("company").value;
            var dataString = 'id='+ id;
            $.ajax
            ({
                type: "POST",
                url: "get_faculty.php",
                data: dataString,
                success: function(html)
                {
                $("#faculty").html(html);
                } 
            });
           }
    
    </script>
    

    Now create another get_faculty.php file in same folder which will be called on onchange event of company dropdown.

    Write below code in that file

        if($_POST['id'])
         {
         $id=$_POST['id'];
    
         $con=mysqli_connect("localhost","username","pass","dbname");
         $sql=mysqli_query($con,"SELECT faculty_name from faculty_table where subject_id = ".$id);
                       while($row=mysqli_fetch_array($sql))
                       {
                           echo $row['faculty_name'];
    
                       }
         }
    

    Dont forget to write mysqli_connect credentials and query accordingly.

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

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入