Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tinkerbell's avatar

How to autocomelete two dropdown list based on each other?

I have assign table for saving the id of three other tables

I have linked the link_id and post_id before for the other two tables, what I want to do now is when user clicked on specific id from requestme a pop up will show to assign link_id and post_id for requestme_id and the saved value of requestme_id will be saved in assign table as request_id

assign table
id  | link_id      |   post_id    | request_id
--- | --------------| -------------   | ------------- 
1   |15 		  | 17 		| null
2   |10 		  | 16 		| null
Reqesutme table

id  
--- 
1   
2 

I want to return the link_id and post_id in two drop down list based on each other when user clicked on (requestme_id)

for example when the user selected 15 in link_id the 17 filled up immediately in the second drop down list since they are related to each other

I can retrieve them dynamically but I want them to be dependent

controller

public function assign($id ,Request $request ){
        $reqesutme = Reqesutme ::find($id);        
$assigns= Assign::get();
return view('reqesutme/assign', compact('reqesutme '  , 'assign'));
}

blade

<div class="col-sm-6">
<label for="post_id">Post </label>
<select class="form-control" id="post_id" name="post_id" required>
    <option></option>
  @foreach($assigns as $assign)
    @if(!empty ($assign->id))
    <option value="{{$assign->post_id }}">
        {{$assign->post_id }}</option>
    @endif
    @endforeach
</select>
</div>

https://imgur.com/ZaJhNXj

to make it more clear see the pic I want to the second one which is post_id filled up directly after choosing Fast_id, and after clicked on submit button the id of request-me saved in assign table as a request_id

0 likes
1 reply
tinkerbell's avatar
tinkerbell
OP
Best Answer
Level 1

This video helped me to achieve what I wanted to do!

https://www.youtube.com/watch?v=c7-HkztGahM&lc=UgxwsYPea6CLz9vep9t4AaABAg.8pWMsySgnyI9J139H_3Tw9&ab_channel=Webslesson

its not working 100%

change the code of the video to this then it will work


public function dynamicDropdown() {
        $countrylist = DB::table('dynamicdropdown')->select('Country')->groupBy('Country')->get();
        return view('dynamicdropdown')->with('countrylist', $countrylist);
    }
    
    
    function fetch(Request $request)
    {
     $select = $request->get('select');
     $value = $request->get('value');
     $dependent = $request->get('dependent');
     $data = DB::table('dynamicdropdown')
       ->where($select, $value)
       ->select($dependent)
       ->groupBy($dependent)
       ->get();
     $output = '<option value="">Select '.ucfirst($dependent).'</option>';
     foreach($data as $row)
     {
      $output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
     }
     echo $output;
    }

Change like this and it will work, you have to use select method,

Please or to participate in this conversation.