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


Creating a dynamic text field that changes content depending on select list choice?

我和我的同事正在创办自己的公司,处理考试成绩和学校数据。我目前正在设置网页(我是 PHP 和 Dreamweaver CC 的新手(,最终用户可以在他们需要的范围内访问和编辑数据库。

但我要说到我的问题。目前,我正在尝试构建一个数据编辑页面,一旦用户登录,所有相关数据都会显示(在这种情况下,这是一个管理员类型的人,所以他/她需要查看哪个老师教哪些课程(。在此页面上,我想构建一个包含所有可能的类的下拉列表(使用记录集完成此操作,按字母顺序排序并正常工作(并链接到该字段,该动态文本字段根据选择列表中选择的选项更改值。

例:

有四个班级,和四个相应的老师,如下所示:

班级 - 教师

英语 - 鲍勃

西班牙语 - 胡安

数学 - 珍妮

地理 - 威廉

我基本上要做的是,如果您在下拉选择列表中选择"英语"类,则文本字段应显示"Bob"(所有其他类也是如此(。到目前为止,我还没有设法弄清楚如何在 Dreamweaver CC 中做到这一点。

编辑:我忘了提到班级名称和相应的老师都是我在记录集中设置的查询的结果 - 所以不必自己制作链接,我必须显示该链接(它为下拉列表中的第一个学校名称这样做,但我认为这是因为查询的性质(

非常感谢帮助!

PHP是一种服务器端语言,而不是客户端语言。您将需要JS而不是PHP来完成此任务。试试这个

<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="" />