使用下拉列表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>

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
    }
}

If you have a select element that looks like this:

<!-- language: lang-html -->
     <input type="hidden" id="depthidden" name="depthidden">
     <select tabindex="3" id="Department">
        <option value="BSEN">BSEN</option>
        <option value="CPSC">CPSC</option>
        <option value="ENGG">ENGG</option>
      </select>

Run this code:

<!-- language: lang-js -->

    var d = document.getElementById("Department");
    document.getElementsById('depthidden').Value = d.options[d.selectedIndex].value;

The above is just a JS demo to help you.

The php section:

$dname = mysql_real_escape_string($_POST['depthidden']);
while($dept= mysqli_fetch_assoc($result)) 
 {
    if($dname==$dept[dname])
       echo "<option value = '".$dept[dname]."' selected>".$dept[dname]."</option>";
    else
       echo "<option value = '".$dept[dname]."'>".$dept[dname]."</option>"; 
 }

The $dname is the dept name to be populated. Otherwise, remove the if and keep the else part.