I have a table named "drivers" with 3 fields "id", "name" and "mob".
I have 2 dropdowns.
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
You need to use ajax initated by on change function on drivers select box. it should look like this :
<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 :
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.
//you can also use id and on 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.
}});
})
I would do this:
Now my friend, I think your question changes with that. Am I right?
I hope it helps!