dsgft1486 2018-11-22 11:34
浏览 165

Combobox基于另一个组合框

I'm trying to do the following. I am selecting an item from a combobox then I want the values of the next combobox to be the result of a query based on the first selection. This is what I have so far.... 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;
}
    ?>

I got stuck when I had to call the PHP function getSelectServiciu(). Also, If there's a better way of doing this, please let me know.

Thanks, Tibi

  • 写回答

1条回答 默认 最新

  • dte66654 2018-11-22 12:10
    关注

    this script is get data from server under a service display as a combobox options

    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 } ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图