在同一个表中,依赖下拉菜单中的代码点火器

时间:2023-02-03 14:13:20

I have a table named "drivers" with 3 fields "id", "name" and "mob".

我有一个名为“司机”的表格,包含三个字段“id”、“姓名”和“暴民”。

I have 2 dropdowns.

我有两个下拉。

  • 1st dropdown: Want to Display Driver's Name form database
  • 下拉1:要显示驱动程序的名称表单数据库
  • 2nd dropdown: Want to Display Mobile no of driver which is selected in 1st dropdown.
  • 2下拉:想要显示在1下拉中选择的驱动的手机号。

Please have a look on my existing code and try to solve my problem. Here is my code:

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

SORRY I am unable to paste code here due to stack overflow error. I have pasted code on this link: here

抱歉,由于堆栈溢出错误,我无法在这里粘贴代码。我在这个链接上粘贴了代码:这里

3 个解决方案

#1


1  

//you can also use id and on jquery funtion

//您还可以使用id和jquery funtion。

 <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


1  

You need to use ajax initated by on change function on drivers select box. it should look like this :

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

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

In your scenario it should be:

在您的方案中应该是:

<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>

Then write a jquery function :

然后编写jquery函数:

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

in your controller:

在你的控制器:

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. 

#3


0  

I would do this:

我想这样做:

  1. Populate the first select (driver) using the PHP foreach (that you already did);
  2. 使用PHP foreach填充第一个select(驱动程序)(您已经这样做了);
  3. Dont populate the second (mobile) select with PHP. Let it empty;
  4. 不要用PHP填充第二个(移动)选择。让它空;
  5. Create a function that gets the value selected on driver select and submit it by ajax;
  6. 创建一个函数,获取在driver select上选择的值并通过ajax提交;
  7. Get the value and populate the mobile select;
  8. 获取值并填充移动选择;

Now my friend, I think your question changes with that. Am I right?

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

I hope it helps!

我希望它可以帮助!

#1


1  

//you can also use id and on jquery funtion

//您还可以使用id和jquery funtion。

 <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


1  

You need to use ajax initated by on change function on drivers select box. it should look like this :

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

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

In your scenario it should be:

在您的方案中应该是:

<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>

Then write a jquery function :

然后编写jquery函数:

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

in your controller:

在你的控制器:

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. 

#3


0  

I would do this:

我想这样做:

  1. Populate the first select (driver) using the PHP foreach (that you already did);
  2. 使用PHP foreach填充第一个select(驱动程序)(您已经这样做了);
  3. Dont populate the second (mobile) select with PHP. Let it empty;
  4. 不要用PHP填充第二个(移动)选择。让它空;
  5. Create a function that gets the value selected on driver select and submit it by ajax;
  6. 创建一个函数,获取在driver select上选择的值并通过ajax提交;
  7. Get the value and populate the mobile select;
  8. 获取值并填充移动选择;

Now my friend, I think your question changes with that. Am I right?

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

I hope it helps!

我希望它可以帮助!