简体   繁体   中英

How to display AJAX results in a HTML drop down menu

I am creating a few drop down menus that populate depending on the previous drop down value selected.

So far I am able to successfully use ajax to grab data from the php script, however, I am not sure how to append that data to the second drop down called 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;

?>
~

So, basically I just need to know how to append the values gathered by AJAX to the drop down menu "areas".

This site helped me big time, also here . Jquery is the way to go. It'll allow you to append data into html and create the dynamic drop-down you're looking for.

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

Depending on how much data you're expecting from your PHP call, you may have to concatenate your results how ever you prefer to get a full list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM