基于另一个组合框的组合框

时间:2018-11-22 11:34:44

标签: javascript php html

我正在尝试执行以下操作。我正在从组合框中选择一个项目,然后我希望下一个组合框的值成为基于第一个选择的查询的结果。这就是我到目前为止所拥有的... HTML

    <fieldset>
<div class="form-group">
    <select class="custom-select" name="serviciuSelect" id="serviciuSelect" onchange="arataAngajat()">
      <option selected="">Selecteaza serviciul dorit</option>
      <?php foreach ($servicii as $item): ?> 
        <option value="<?php echo $item['denumire']; ?>"><?php echo $item['denumire']; ?></option>
      <?php endforeach;?>
    </select>
 </div>
</fieldset>

<fieldset>
<div class="form-group">
    <select class="custom-select" name="angajatSelect" id="angajatSelect" style="visibility: hidden">
      <option selected="">Selecteaza angajatul dorit</option>
      <!-- <?php foreach ($angajati as $item): ?> 
        <option value="<?php echo $item['nume']; ?>"><?php echo $item['nume']; ?></option>
      <?php endforeach;?> -->
    </select>
 </div>
</fieldset>

JavaScript

    <script>
    function arataAngajat() {
        document.getElementById("angajatSelect").style.visibility = "visible";
        var e = document.getElementById("serviciuSelect").value;
        document.cookie = "selectSer = " + e;  
   }
   </script>

PHP

    <?php 
function getSelectServiciu(){
    $selectServiciu= $_COOKIE['selectSer'];
    $query = "SELECT COD_A FROM angajat WHERE COD_A IN (
    SELECT COD_A FROM angajat_serviciu WHERE COD_S='".
    $selectServiciu."')";
    $result = $this->db->query($query)->result_array();
    return $result;
}
    ?>

当我不得不调用PHP函数getSelectServiciu()时,我陷入了困境。另外,如果有更好的方法,请告诉我。

谢谢, 提比

1 个答案:

答案 0 :(得分:0)

此脚本是在服务显示下从服务器获取数据作为组合框选项

services.php

<?php

//i'm using array u can use mysql array
$services=array();
$services[]=array('name'=>'Flowers');
$services[]=array('name'=>'Fruits');

//data under services
$data=array();
$data['Flowers']=array('Rose','Belly');
$data['Fruits']=array('Banana','Mango');

//check the the client is requested for data under a service
if(isset($_GET['get_data'])){
$current_service=$_GET['get_data'];
$data_array=$data[$current_service];
echo  '<option selected="">Select</option>';
foreach($data_array as $op){
echo  '<option value="'.$op.'">'.$op.'</option>';
}
}else{
?>
 <fieldset>
<div class="form-group">
    <select class="custom-select" name="Services" id="Services" onchange="Getnext_option(this.value)">
      <option selected="">Select</option>
      <?php foreach ($services as $item): ?> 
        <option value="<?php echo $item['name']; ?>"><?php echo $item['name']; ?></option> <?php endforeach;?>
    </select>
 </div>
</fieldset>

<fieldset>
<div class="form-group">
    <select class="custom-select" name="Loaded_data" id="Loaded_data" style="display: none"> </select>
 </div>
</fieldset>

<script>function Getnext_option(id){
var load_to=document.getElementById('Loaded_data');
load_to.style.display="none";
var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      load_to.innerHTML=
      this.responseText;
        load_to.style.display="block";
    }
  };
  xhttp.open("GET","services.php?get_data="+id, true);//get data from server
  xhttp.send();
}</script>

<?php } ?>