Hi i have this select dropdown in Ajax pass the data to the server then display to my select dropdown. The code works very well. Now my problem is i want that upon selecting the data the other select dropdown will refresh base on the selected one. Here is my Ajax code below
$("#util").change(function(){
const util = $(this).children("option:selected").val();
//make ajax call
$.ajax({
type: "GET",
url:'/dno-personal/get-data/' + util,
data:{
_method: 'get',
"id":util
},
success:function(data){
var docu = $('#docu');
console.log(data);
data.forEach(function(val, index){
console.log(val.document_name);
docu.append(
$('<option></option>').val(val.document_name).html(val.document_name)
);
});
},
error:function(data){
console.log('Error:', data);
}
});
});
my html select
<div id="documentList" class="col-lg-2">
<label>Document List</label>
<select id="docu" name="" class="selcls form-control">
</select>
</div>
my process in server in php
//
public function getData($id){
$getDocuments = DnoPersonalUtility::where('pu_id', $id)->get()->toArray();
return response()->json($getDocuments);
}
Can someone help me figured this thing out? Tia
2
Answers
In this case, first, select the value on the basis of which you want your second dropdown values to be changed via var id = e.options[e.selectedIndex].value; then on click of your first dropdown post that value to ajax and as per your code appends the value to your second dropdown.
This is the script I used