dongsi7067 2014-12-15 03:47
浏览 46
已采纳

使用下拉列表html中的选定值

I am trying to get the value from a drop down menu (list) and use that value to create another drop down menu. I have a database in mysql, in that database is a list of departments and then each department has a list of courses. What I am trying to do is have a drop down list for the departments which I have already created and then use the value the user selected to make a drop down list for the courses.

EDIT: Updated code:

JavaScript dropmysql.js

<!-- start: Container -->
<div class="container">

  <!-- start: Contact Form -->
  <div class="title">
    <h4>Contact Form</h4>
  </div>

  <!-- start: Contact Form -->
  <div id="contact-form">

    <form method="post" action="assets/upload.php" enctype="multipart/form-data">

      <fieldset>
        <div class="clearfix">
          <label for="name"><span>Name:</span>
          </label>
          <div class="input">
            <input tabindex="1" size="18" id="name" name="name" type="text" value="">
          </div>
        </div>

        <div class="clearfix">
          <label for="email"><span>Email:</span>
          </label>
          <div class="input">
            <input tabindex="2" size="25" id="email" name="email" type="text" value="" class="input-xlarge">
          </div>
        </div>

        <div class="dropdown">
          <label for="department"><span>Department:</span>
          </label>
          <select tabindex="3" type="text" name="Department">
            <?php while($dept=m ysqli_fetch_array($result)) { echo "<option value=\" ".$dept['dname']."\ ">".$dept[ 'dname']. "</option>"; } //<option value="volvo">Volvo</option>
            ?>
          </select>
        </div>
        <div class="dropdown" id="course-container">
          <!-- SELECT WILL BE PLACED USING JAVASCRIPT -->
        </div>

        <div class="actions">
          <p>
            <label for="file"><span>Select file to upload:</span>
            </label>
            <input tabindex="8" type="file" name="fileToUpload" id="fileToUpload">
          </p>
          <p>
            <input tabindex="3" type="submit" value="Upload File" name="submit" class="btn btn-succes btn-large">
          </p>
        </div>

      </fieldset>
    </form>

  </div>
  <!-- end: Contact Form -->
</div>
<!-- end: Container -->
<!-- start: Java Script -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery-1.8.2.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/flexslider.js"></script>
<script src="js/carousel.js"></script>
<script src="js/dropmysql.js"></script>
<script def src="js/custom.js"></script>
<!-- end: Java Script -->

PHP upload.php

<?php
    include ("../config/database.php");

    if (isset($_POST) && !empty($_POST)) {
        $department = $_POST['department'];

        $query = "SELECT * FROM courses WHERE dname = '{$department}'";

        while (mysql_fetch_assoc($query)) {
            echo '<option value="{$value}>{$name}</option>"'; // Loop through the database again and echo them here
        }
    }
?>

PHP ddown.php:

<?php
include ("../config/database.php");

if (isset($_POST) && !empty($_POST)) {
    $department = $_POST['department'];

    $query = "SELECT * FROM courses WHERE dname = '{$department}'";

    while (mysql_fetch_assoc($query)) {
        echo '<option value="{$value}>{$name}</option>"'; // Loop through the database again and echo them here
    }
}

?>

</div>
  • 写回答

2条回答 默认 最新

  • dongne1560 2014-12-15 04:16
    关注

    First it will be helpful to add an id/class to the first select such as:

    <select tabindex="3" type="text" name="Department" id="department">
        <option value="BSEN">BSEN</option>
        <option value="CPSC">CPSC</option>
        <option value="ENGG">ENGG</option>
    </select>
    

    You'll need to setup the 2nd dropdown list with empty options, or setup an empty div similar to this

    <div id="course-container">
       <!-- SELECT WILL BE PLACED USING JAVASCRIPT -->
    </div>
    

    Then a javascript file similar to this could be used:

    $(function() {
        var course_container = $("div#course-container");
        var data = {};
    
        $('select#department').change(function() {
            $(course_container).html("<select id=\"courses\"></select>");
            var courses = $("select#courses");
            var d = document.getElementById("department");
            data['department'] = d.options[d.selectedIndex].value;
    
            $.ajax({
                url: "path/to/php/script.php",
                type: "post",
                data: data,
                success: function(response) {
                    $(courses).innerHTML = response;
                }
            });
        });
    });
    

    Then just a php script like this:

    <?php
    
    if (isset($_POST) && !empty($_POST)) {
        $department = $_POST['department'];
    
        $query = "SELECT * FROM `DATABASE` WHERE `DEPARTMENT` = {$department}";
    
        while (mysql_fetch_assoc($query)) {
            echo '<option value="{$value}>{$name}</option>"'; // Loop through the database again and echo them here
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog