0

我正在创建一些下拉菜单,这些下拉菜单根据之前选择的下拉值填充。

到目前为止,我能够成功地使用 ajax 从 php 脚本中获取数据,但是,我不确定如何将该数据附加到名为 AREAS 的第二个下拉列表中。

HTML / Javascript / AJAX
    <html>
    <head>
      <title>IPSLA Report</title>
    <script type="text/javascript">
    function changeSelections() {
      var departments = document.selections.department;
      var areas = document.selections.areas;
      var months = document.selections.months;
      var years = document.selections.years;
      var s = document.getElementById("department");

      switch(departments.selectedIndex) {
        case 0:
          areas.options.length = 0;
          months.options.length = 0;
          years.options.length = 0;

          areas.options[0] = new Option("Select an Area");
          months.options[0] = new Option("Select a Month");
          years.options[0] = new Option("Select a Year");
          departments.options[0].selected = true;
          break;

        default:
          months.options.length = 0;
          years.options.length = 0;

          months.options[0] = new Option("Select a Month");
          years.options[0] = new Option("Select a Year");

          var pass = s.options[s.selectedIndex].text;
          ajaxFunction(pass);
      }

    }


    function ajaxFunction(pass) {

    var ajaxRequest;
      try {
         ajaxRequest = new XMLHttpRequest();
      }
      catch(e) {
        try {
          ajaxRequest = new ActiveXObjext("Msxml2.XMLHTTP");
        }
        catch(e) {
          try {
            ajaxRequest = new ActiveXObjext("Microsoft.XMLHTTP");
          }
          catch(e) {
            alert("Please use another browser");
            return false;
          }
        }
      }
      ajaxRequest.onreadystatechange = function() {
        if (ajaxRequest.readyState == 4) {
          var ajaxDisplay = document.getElementById("areas");
          if (ajaxDisplay != null) {
          ajaxDisplay.options.selectedIndex.text = ajaxRequest.responseText;
          }
          else {
            document.write("NULL!!!");
          }
        }
      }

      if (pass == "CRAN") {
        var active_id = '0';
      }

      var queryString = "?active_id=" + active_id;
      ajaxRequest.open("GET","db_connect.php" + queryString, true);
      ajaxRequest.send(null);

    }
    </script>
    </head>
    <body>
    <div id="wrapper">
      <div id="select">
        <form name="selections" id="selections" action="">
          <select name="department" id="department" onChange="changeSelections()">
            <option value="none">Select Department</option>
            <option value="none">CRAN</option>
            <option value="none">BackBone</option>
            <option value="none">Datacenter</option>
            <option value="none">Enterprise</option>
          </select>


          <select name="areas" id="areas" onChange="changeMonth()">
            <option value="none">Select an Area</option>
          </select>

          <select name="months" id="months" onChange="changeYear()">
            <option value="none">Select a Month</option>
          </select>

          <select name="years" id="years" onChange="go(this)">
            <option value="none">Select a Year</option>
          </select>
        </form>
      </div>


      <div id="image">

      </div>


      <div id="incidents">

      </div>
    </div>
    </body>
    </html>

PHP Script
<?php
  $host = "";
  $dbName = "";
  $username = "";
  $password = "";

  $conn = mysql_connect($host,$username,$password);
  $db = mysql_select_db($dbName,$conn);

  $dept_id = $_GET['id'];

  $area_query = "SELECT area_name FROM incident_area WHERE FK_dept= '$dept_id'";
  $area_result = mysql_query($area_query);

 $options = "";
  while ($area_row = mysql_fetch_assoc($area_result)) {
     #$options .= '<option value="'.$area_row['area_name'].'">'.$area_row['area_name'].'</option>';
      $options .= $area_row['area_name'];
  }
 echo $options;

?>
~

所以,基本上我只需要知道如何将 AJAX 收集的值附加到下拉菜单“区域”。

4

1 回答 1

1

这个网站帮了我很大的忙,也在这里。Jquery是要走的路。它允许您将数据附加到 html 并创建您正在寻找的动态下拉列表。

Javascript
// needs hotlink to jquery file, for library to work right. 
var options = "";
$(document).ready(function(){
    $.post("./file.php", { postVariableInPHPFile: "some string" }, function(data) {
        options = data; // data is the return from the AJAX call 
    });
});

根据您希望从 PHP 调用中获得多少数据,您可能需要将结果连接起来,以获得完整列表。

于 2012-04-23T00:46:14.567 回答