I am trying to load information such as ‘name’ from a table where the admission number matches the one selected from the select box using jquery. With the code below, it only select the first ‘name’ from the table in the database and it doesn’t select with respect to the admission number on the select box. (Note: I am not very familiar with jquery)
Code for the Route.
Route::post('get-student-info/', [ResultController::class, 'get_student_info']);
Controller code
public function get_student_info(Request $request)
{
$adnum=$request->post('admission_num');
$stud=User::where('admission_num',$adnum)->get();
foreach ($stud as $list)
{
$html='<p><b>First Name :'.$list->name.'</b></p>';
}
echo $html;
}
Blade.php code
<div class="col-md-3 ms-3">
<select class="form-select form-select-sm" name="admission_num" id="admission_num" aria-label="Default select example">
<option></option>
@foreach($admission as $admissions)
<option value="{{ $admissions->admission_num }}">{{ $admissions->admission_num }}</option>
@endforeach
</select>
</div>
Output shows here
<div id="info" class="receipt-right">
<p><b>First Name :</b></p>
<p><b>Last Name :</b></p>
</div>
JQuery Code
<script>
jQuery(document).ready(function(){
jQuery('#admission_num').change(function(){
let adnum=jQuery(this).val();
jQuery('#info').html('<p><b>First Name :</b></p>')
jQuery.ajax({
url:'/get-student-info',
type:'post',
data:'adnum='+adnum+'&_token={{csrf_token()}}',
success:function(result){
jQuery('#info').html(result)
}
});
});
});
</script>
2
Answers
Seems like no issue in jquery part. I see the issue is in the Controller.
After you are getting relevant results using admission number and loop it for a output. but in the loop it always give us to the last record (according to your code)
try this way.