Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
dependent dropdown in code igniter in same table

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

I have 2 dropdowns.

  • 1st dropdown: Want to Display Driver's Name form database
  • 2nd dropdown: Want to Display Mobile no of driver which is selected in 1st dropdown.

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

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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. 
about 4 years ago · Santiago Trujillo Report

0

//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. 
                    }});
            })
about 4 years ago · Santiago Trujillo Report

0

I would do this:

  1. Populate the first select (driver) using the PHP foreach (that you already did);
  2. Dont populate the second (mobile) select with PHP. Let it empty;
  3. Create a function that gets the value selected on driver select and submit it by ajax;
  4. Get the value and populate the mobile select;

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

I hope it helps!

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!