pass value from select option to another field in the same view blade

Viewed 30

I am not so sure if this is applicable or not, I have a scenario where I am getting values from database into select option inside a blade.

<select name="region" id="region" onchange="selectRegion()">
   @foreach ( $regions as $region)  
     <option value="{{ $region->region_id }}">{{ $region->region_name }}</option>       
   @endforeach
</select> 

I have another field in the same view which I want to display districts from the selected region above. tables region and district have relationship in the database.

 <label>Districts</label>
 @if($valuefromabove)
   @php 
   $districts = \App\District::where(['region_id' => $valuefromabove])->get();
   @endphp
   <select name="district" id="district">
      @foreach ( $districts as $district)  
        <option value="{{ $district->district_id }}">{{ $district->district_name }}</option>       
     @endforeach
 </select> 
 @endif

So I am thinking of the possible way to pass value from select option to district field and to pupulate District's select option corresponding to the value from region.

I tried to get the selected value with javaScript like below

   <script>
   function selectRegion(){
        let regionSelected = document.getElementById("region").value;
        console.log(regionSelected);
   </script>

but unfortunately I couldn't transfer it back to php.

What is the proper way recommended to handle this. Thanks in advance.

0 Answers
Related