dongtuo3795 2013-09-30 09:32
浏览 57
已采纳

创建一个动态文本字段,根据选择列表选项更改内容?

Me and my colleague are in the process of starting up our own company that handles exam results and school data. I am currently in the process of setting up the webpage (I am completely new with PHP and Dreamweaver CC) where the end-user can access and edit the database in the extent that they need to.

But I shall get to the point of my problem. At the moment I am trying to build a data edit page where all relevant data is shown once the user has logged in (In this case it's an admin-kind of person, so he/she needs to see which teacher teaches which classes). On this page I want to build a drop-down list of all the possible Classes (done this with the recordset, sorted alphabetically and working) and linking to that, a dynamic text field that changes value depending on the option picked in the select list.

Example:

There are four classes, and four corresponding teachers as followed:

Class - Teacher

English - Bob

Spanish - Juan

Math - Jenny

Geography - William

What I'm basically after is that if you choose the Class 'English' in the drop-down select list, the text field should display 'Bob' (and similarly for all the other classes). So far I've not managed to work out how to do that in Dreamweaver CC.

EDIT: I forgot to mention that both the class names and the corresponding teachers are a result from the query I've set up in a recordset - so instead of having to make the link myself I have to display that link (it does so for the very first school name in the dropdown list, but I think that's because of the nature of the query)

Help is much appreciated!

  • 写回答

1条回答 默认 最新

  • duanbiaoshu2021 2013-09-30 09:37
    关注

    PHP is a server side language; not a client side one. You're gonna need JS and not PHP for this task. Try this

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
    <script type="text/javascript">
       $('.teacher').change(function(){
          var teacher = $(this).val();  
          $('#text').val(teacher);
       });
    </script>
    
    <?php
        //If all of the info for the classes is stored in MySQL, then do this
        //I'm not sure how your DB is structured, but you should get the idea here
        //Query the DB for all of the teachers and the subjects they teach
        $con = mysqli_connect("localhost", "root", "password", "database");
        $result = mysqli_query($con, "SELECT * FROM classes");
        $count = mysqli_num_rows($result);
        $i = 0;
    
       while($row = mysqli_fetch_array($result)) {
          $teacher[$i] = $row['teacher'];
          $subject[$i] = $row['subject'];
    
          $i++;
       }
    ?>
    <select class="teacher">
    <?php
        //Loop thru and for each row, echo out an option tag
        for($i=0;$i<$count;$i++) {
            echo "<option value='".$teacher[$i]."'>".$subject[$i]."</option>";
        }
    ?>
    </select>
    
    <input type="text" id="text" value="" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀