从数据库动态填充双列表框

时间:2016-09-28 01:21:06

标签: javascript php jquery html ajax

我一直试图根据在其他选择上做出的选择来填充我的双列表框。要使用的选项来自数据库。 代码正在运行,如果我将innerHTML部分设置为普通的单一选择,但如果我将其设置在双列表框ID上则无法工作。

代码:

的index.php

//html
<select id="jobSpecialization" class="form-control" name="jobSpecialization" required onchange="fetch_select(this.value);">
  <option value=""></option>

  <?php 
    $getSpecializations = mysqli_query($con,"call all_specializations()");
    while($r=mysqli_fetch_array($getSpecializations)){
      echo "<option value='".$r['specializationId']."'>".$r['specializationTitle']."</option>";
    }
  ?>

</select>

<select multiple="multiple" size="10" name="duallistbox_demo1[]" id="skillsListBox"> 
</select>

//script

<script type="text/javascript">
  function fetch_select(val)
  {
    $.ajax({
    type: 'post',
    url: 'getter.php',
    data: {
      get_option:val
    },
    success: function (response) {
      document.getElementById("skillsListBox").innerHTML = response; 
    }
    });
  }
</script>

getter.php

<?php

include '../resources/includes/conn.php';

$sid = $_POST['get_option'];

$skillsTaggedQuery = mysqli_query($con,"call get_specialization_info('".$sid."')");
mysqli_next_result($con);
$row = mysqli_fetch_array($skillsTaggedQuery);
$skillsTagged = $row['skills'];

$sql = mysqli_query($con,"SELECT skillTitle FROM tbl_skills");

$st = explode('|' , $skillsTagged);

$count = count($st);

$x = "";

while($r = mysqli_fetch_array($sql)){

    for($i=0;$i<$count;$i++){
            if($r['skillTitle'] == $st[$i]){
                        $x = 1;
            }
    }

    if($x == 1){
            echo "<option selected value='".$r['skillTitle']."'>".$r['skillTitle']."</option>";
    }
    else{
            echo "<option value='".$r['skillTitle']."'>".$r['skillTitle']."</option>";
    }

    $x = "";

}


?>

0 个答案:

没有答案