Why use collect instead of just returning the DB results to the view?
Also check the response in your network tab.
I am new at this and I could be missing something very basic or have it totally wrong. I have been through some basic tutorials and some things here and Googled around that has gotten me this far.
I have two select boxes in my view (qryStructures_start). The first 'select' I populate via a query in my controller. I have another function in that controller that gets data for my second 'select' to be populated based on whatever is selected in the first box via Ajax.
I know the query works because I am converting an existing form that I built to use Ajax instead.
Route
Route::get( 'ResearchReports/ResearchDatabase/field/qryStructures_start', [\App\Http\Controllers\FieldStructures::class, 'structuresStart'] );
Route for Ajax
Route::get( 'ResearchReports/ResearchDatabase/field/qryStructures_start?site={site}', [\App\Http\Controllers\FieldStructures::class, 'suAjax'] );
Controller
public function suAjax(Request $request)
{
my query...
$ajaxDataSql = "SELECT Blah FROM Blah...."
$ajaxDataSql = DB::getPdo()->prepare($ajaxDataSql);
$ajaxDataSql->setFetchMode(\PDO::FETCH_OBJ);
$ajaxDataSql->execute([]);
$ajaxData = collect($ajaxDataSql->fetchAll());
return response()->json($ajaxData);
In my view, the first box - populates from a different function in the same controller
@if( $selectSite )
<select id="site" name="site[]" size="5" multiple>
@foreach( $selectSite as $site)
<option value="{{ $site->Site }}">{{ $site->SiteName }} ({{ $site->Site }})</option>
@endforeach()
</select>
@endif
Next box - the Ajax
$('#site').change(function(){
var site = $(this).val();
if(site){
$.ajax({
type:"GET",
url:"{{url('/ResearchReports/ResearchDatabase/field/qryStructures_start')}}?site=" + site,
dataType: "json",
success:function(sut)
{
if(sut.error)
{
console.log(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>');
});
}
}
});
}
});
The box I want Ajax to populate
<select id="suTyp" name="SUTyp[]" size="7" multiple ></select>
So what works, var site does return the site number when I click on the site, I can see it in the console. After I click and get the site though, nothing happens. Nothing in the console, nothing on the page, nothing.
Thanks for the help!
SOLVED - I was able to get it to populate the Select box. I am posting what I did here to hopefully help anyone else (provided this is a best practice). I did it by by using this on the page with my form:
$('#site').change(function(){
var site = $(this).val();
if(site){
$.ajax({
type:"get",
url:"{{url('/ResearchReports/ResearchDatabase/field/qryStructures_start_ajax')}}",
data: 'site=' + site,
dataType: "json",
beforeSend: function () {
spinner.addClass('loading');
},
success: function(sut) {
// Check if anything is in the Json
if(Object.keys(sut).length !== 0)
{
spinner.removeClass('loading');
$("#suTyp").empty();
$("#suTyp").append('<option>Select Study Unit Type(s)</option>');
$.each(sut, function (index, item) {
$("#suTyp").append('<option value="' + item.SUDescCode + '">' + item.ESR_SUcat + '</option>');
});
} else {
spinner.removeClass('loading');
$("#suTyp").empty();
$("#suTyp").append('<option>No Study Units Found</option>');
}
},
complete: function () {
spinner.removeClass('loading');
},
});
}
});
(I added a spinner overlay as the next box loads using CSS)
It appends the select box here:
<select id="suTyp" name="SUTyp[]" size="7" multiple ></select>
This posts it to a URL that is in my Route (this is only a URL, not an actual page):
Route::get( 'ResearchReports/ResearchDatabase/field/qryStructures_start_ajax', [\App\Http\Controllers\FieldStructures::class, 'suAjax'] );
My function runs the query and sends it back to my Ajax:
$ajaxDataSql = "SELECT tlkpSUDesc.ESR_SUcat, tlkpSUDesc.SUDescCode blah blah.."
$ajaxDataSql = DB::getPdo()->prepare($ajaxDataSql);
$ajaxDataSql->setFetchMode(\PDO::FETCH_OBJ);
$ajaxDataSql->execute([]);
$ajaxData = collect($ajaxDataSql->fetchAll());
return response()->json($ajaxData);
This is the data I get in the console.log that is sent back to my Ajax:
[
{
"ESR_SUcat": "Noncultural Deposits",
"SUDescCode": 35
},
{
"ESR_SUcat": "Void",
"SUDescCode": 97
}
]
My box populates. If anyone has a better way or I am doing something I maybe I shouldn't I am happy to learn.
Please or to participate in this conversation.