Which output do you get in your console from this log console.log(sut); ?
Passing DB Query via Ajax to Populate Options Based on Previous Selection
I am new at this and I could be missing something very basic. I have two select boxed in my view. The first 'select' I populate via a query in my controller. I have another query in that same function that gets data for my second 'select' to be populated based on whatever is selected in the first box via Ajax.
Route:
Route::get( 'qryStructures_start', [\App\Http\Controllers\FieldStructures::class, 'structuresStart'] )
Controller, at the end of my SQL statement that I need to populate the second 'select' box:
$ajaxDataSql = DB::getPdo()->prepare($ajaxDataSql);
$ajaxDataSql->setFetchMode(\PDO::FETCH_OBJ);
$ajaxDataSql->execute([]);
$ajaxData = collect($ajaxDataSql->fetchAll());
$ajaxData = response()->json($ajaxData);
If I dump $ajaxData I get data like this:
+headers: Symfony\Component\HttpFoundation\ResponseHeaderBag {#66144 ▶}
#content: "[{"Site":"5MT1825","ESR_SUcat":"Kivas","ESR_SUcatdesc":"kivas.","SUDescCode":10},{"Site":"5MT1825","ESR_SUcat":"Rooms","ESR_SUcatdesc":"rooms.","SUDescCode":20} ▶"
#version: "1.0"
#statusCode: 200
#statusText: "OK"
#charset: null
#data: "[{"Site":"5MT1825","ESR_SUcat":"Kivas","ESR_SUcatdesc":"kivas.","SUDescCode":10},{"Site":"5MT1825","ESR_SUcat":"Rooms","ESR_SUcatdesc":"rooms.","SUDescCode":20} ▶"
#callback: null
#encodingOptions: 0
+original: Illuminate\Support\Collection {#66153 ▶}
+exception: null
or if I use return response()->json($ajaxData); it just shows regular json on the view when I load it.
In my view I have:
<select id="site" name="site[]" size="5" multiple>
@foreach( $selectSite as $site)
<option value="{{ $site->Site }}">{{ $site->SiteName }} ({{ $site->Site }})
@endforeach()
</select>
ajax:
$('#site').change(function(){
var siteID = $(this).val();
if(siteID){
$.ajax({
type:"get",
data:"{{url('ResearchReports/ResearchDatabase/field/qryStructures_start)}}"+ siteID,
success:function(sut)
{
console.log(sut);
if(sut)
{
$("#suTyp").empty();
$("#suTyp").append('<option>Select Study Unit Type</option>');
$.each(sut,function(key,value){
$("#suTyp").append('<option value="'+key+'">'+value+'</option>');
});
}
}
});
}
});
I am new so not too sure what I am missing but the next select box doesn't populate with anything other than "Select Study Unit Type" and I just get the view code in my console. Can someone point me in the right direction?
Thanks!
Please or to participate in this conversation.