After selecting an option from drop down list, I am submitting it to a controller function which is returning required data in a different view. I want it to load the view in the same page (possibly inside a div) as the drop down list where I am submitting it. (I am new to Laravel and PHP)
The following is my clinic.blade.php file which lists clinics in drop down.
{!! Form::open(['route' => 'clinicIndex', 'method' =>
'POST'])!!}
<label> Select Clinics </label>
<select name = "clinic">
@foreach($clinics as $clinic)
<option value="{{$clinic->clinicID}}"> {{$clinic->clinicName}}</option>
@endforeach
</select>
{{Form::submit('DisplayDoctors', ['class' => 'btn btn-outline-
primary'])}}
{{Form::close()}}
The Submit is accessing my controller method via clinicIndex Route defined as below in web.php
Route::post('/clinicTest', 'ClinicController@clinicIndex')-
>name('clinicIndex');
This is the function in controller
public function clinicIndex(Request $request)
{
$selectedClinic = $request->clinic;
$clinicInfo = Clinic::where('id', '=', $selectedClinic)->get();
$dbArray = DB::connection('mysql2')->select("SELECT * FROM bp_admin.tdbsrv WHERE iClinicId = $selectedClinic");
$dbInfo = $dbArray[0];
$remoteConnection = DatabaseConnection::setConnection($dbInfo);
$doctors = $remoteConnection->select("SELECT tUsers.sSalutation, tLocationPhysician.iPhysicianId, tUsers.sFirstName,
tUsers.sLastName,tUsers.iPhysNum,tUsers.sDateHired,
tPhysician.bLocum,tPhysician.bResident,
tPhysician.dStartDay,tPhysician.dEndDay
from tUsers
inner join tLocationPhysician
inner join tPhysician
where
tLocationPhysician.iLocationId = $selectedClinic
and tPhysician.iId = tLocationPhysician.iPhysicianId
and tPhysician.iPhysNum = tUsers.iPhysNum
and tUsers.bArchived =0
and tLocationPhysician.bArchived =0
and tPhysician.bArchived =0
order by tUsers.sFirstName asc");
return view('pages.show')->with('doctors', $doctors);
}
You can see that it is returning the table in a different view (opens a new page). Instead I want it to load the table in same view after submitting my selection from drop down.
I tried doing this in my clinic.blade.php
<div class="container" id="dispDoctors"> </div>
And added an ajax script in the same view
<script>
function loadDocContent(){
$('#dispDoctors').load('/loadDocs');
}
</script>
For testing dynamic load, I defined the function for "/loadDocs" same as my controller function clinicIndex() in web.php. I am stuck how to proceed from here. As I am not sure how to pass my drop down selection to the jquery function.