【问题标题】:dependent dropdown in code igniter in same table同一张表中代码点火器中的相关下拉列表
【发布时间】:2017-03-21 21:23:06
【问题描述】:

我有一个名为“drivers”的表,其中包含 3 个字段“id”、“name”和“mob”。

我有 2 个下拉菜单。

  • 第一个下拉菜单:想要显示驾驶员姓名表单数据库
  • 第二个下拉列表:想要显示在第一个下拉列表中选择的驱动程序的手机号。

请查看我现有的代码并尝试解决我的问题。 这是我的代码:

抱歉,由于堆栈溢出错误,我无法在此处粘贴代码。我已在此链接上粘贴代码:here

【问题讨论】:

  • 我已经尽我所能改善这个问题,但仍然不清楚。
  • 我想现在我想问什么已经很清楚了。

标签: javascript jquery json ajax codeigniter


【解决方案1】:

//也可以使用id和jquery函数

 <select name="dname" class="form-control" id="selectid">
    foreach($results_drivers as $row)
     {
      echo '<option value="'.$row->id.'">'.$row->name.'</option>';
     }
 </select> 

 get value using select box id, post driver id in controller method using            
 ajax function call model method in your controller method and put this        
 driver id in your model method's parameter and fetch reult data in success funtion     

    $('#selectid').on('change', function() {
      var value = this.value; 
      var val;                                  
    var url = <?php echo base_url('admin/new_booking_validation'); ?> 
        $.ajax({
        url: url,
       type: 'POST',
       data: { val: value}, 
       success: function(result){
       //now populate the mobile number in the select box. 
                    }});
            })

【讨论】:

    【解决方案2】:

    您需要在驱动程序选择框上使用由 on change 函数启动的 ajax。它应该是这样的:

    <select onchange="getval(this.value)">
    <option value="1">One</option>
    <option value="2">Two</option>
    </select>
    

    在你的场景中应该是:

    <select name="dname" class="form-control" onchange="getval(this.value)">
    // <?php
    
    foreach($results_drivers as $row)
    { 
     echo '<option value="'.$row->id.'">'.$row->name.'</option>';
    
    }
    ?>
    </select>
    

    然后写一个jquery函数:

    function getval(driverid){
         $.ajax({url: "controller/newmethod/"+driverid, success: function(result){
        //now populate the mobile number in the select box. 
    }});
    }
    

    在你的控制器中:

    Public function newmethod($id){
    $driver_data = //query to get data from your table with where cluase
    /// where id = $id
    //and then get the data in format you like, let say array 
    print_r($driver_data);exit;
    }
    
    Now in jquery , you have data of driver in result variable that is passed as a parameter in function . now that should be easy for you . if you still have question or problem implementing it , let me know. It is easy task and you are going to use it in future a lot of times. Thats why I want you to do it your self and just telling you the methodology. 
    

    【讨论】:

    • 我应该在“url to your controller method/”中写什么我已经放了 因为整个代码都在这个方法中。但它不起作用。
    【解决方案3】:

    我会这样做:

    1. 使用 PHP foreach 填充第一个选择(驱动程序)(您已经这样做了);
    2. 不要使用 PHP 填充第二个(移动)选择。让它清空;
    3. 创建一个函数,获取在 driver select 上选择的值并通过 ajax 提交;
    4. 获取值并填充移动选择;

    现在,我的朋友,我认为您的问题会随之改变。我说的对吗?

    希望对你有帮助!

    【讨论】:

    • 提示:对每个申请使用不同的控制器方法。您的页面响应控制器内的方法,而您的 ajax 响应同一方法内的另一个控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2016-05-09
    • 2019-02-26
    相关资源
    最近更新 更多