Level 2
I know this might sound silly, but have you checked your datatypes?
Your switch statement is checking for integers and your drop down values will be sent through as strings.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
The network in developer tools shows that the selection is being made and gives me a preview of what is the correct code but the actual live code does not update when the select option is changed, any ideas on why this is happening and how to fix the issue so that the live code changes?
PHP:
$batsmenQuery = Batsmen::where('approved', '=', 1);
switch ($request->SortbyList){
case 0:
$batsmenQuery = $batsmenQuery->orderBy('name', 'DESC');
break;
case 1:
$batsmenQuery = $batsmenQuery->orderBy('name', 'ASC');
break;
case 2:
$batsmenQuery = $batsmenQuery->orderBy('hs', 'ASC');
break;
case 3:
$batsmenQuery = $batsmenQuery->orderBy('hs', 'DESC');
break;
default:
$batsmenQuery = $batsmenQuery->orderBy('name', 'DESC');
}
$batsmen= $batsmenQuery->paginate(40);
HTML:
<div class="row">
<div class="sort">
<select name="SortbyList" id="SortBy">
<option value="0">A to Z</option>
<option value="1">Z to A</option>
<option value="2">Highest Score</option>
<option value="3">Lowest Score</option>
</select>
</div>
</div>
@foreach($batsmen as $batsman)
<h1> {{$batsman->name}} <h1>
<h5> {{$batsmen->score}}<h5>
@endforeach
JavaScript:
$(document).ready(function() {
$('#SortBy').on('change', function(e) {
$.ajax({
url: "{{route('search.index')}}",
data: {SortbyList: this.value},
dataType: "json",
success: function (result) {
if (result) {
$("#SortBy").empty();
var new_options = '';
$.each(result, function (k, v) {
new_options += '<option value="' + v.value + '">' + v.name + '</option>'
});
$("#SortBy").append(new_options);
}
}
});
});
Please or to participate in this conversation.