php - How to reload page on select option Ajax get method
I have a list of competitions in my select option,
I need to know how can make this functionality work,
When a user selects a competition the page should reload with the competition data,
This is the select option in my blade
<div class="row pt-3">
<div class="col">
<select name="competition" id="competitionSearch" class="select listing pt-2 pb-2" >
<option value="">Select Competition</option>
@foreach ($allCompetitions as $competition)
<option value="{{ $competition->id }}"> {{ $competition->name }}</option>
@endforeach
</select>
</div>
</div>
this is how it looks like
this is the ajax get method i have used
$(document).ready(function(){
$('#competitionSearch').change(function(){
var compid = $(this).val();
if(compid > 0){
fetchRecords(compid);
}
});
});
function fetchRecords(id){
$.ajax({
url: 'detailed-registrations/getCompetitionAjax/'+id,
type: 'get',
success: function(response){
if (response) {
//load selected competition here
}
}
});
}
My routes
Route::get('competitions/{competition}/detailed-registrations/getCompetitionAjax/{id}','CompetitionController@getCompetitionAjax');
My function in the controller
public function getCompetitionAjax($competition, $id, Request $request)
{
$comp = $this->competitionsRepo->findOrFail($id);
return redirect(route('competitions.detailed-registrations',$id))->with('comp',$comp);
}
I need to know how can i make this functionality work
initially the user will be in a competition page
http://127.0.0.1:8000/competitions/1/detailed-registrations
once he selects the competition 2 redirected to the competition page with the filtered query parameters as
http://127.0.0.1:8000/competitions/2/detailed-registrations?filters=visible&age=all&gender=all
Answer
Solution:
Don't think you need an ajax call for redirecting the user to a different page when a competition is selected, you can redirect from javascript
$(document).ready(function(){
$('#competitionSearch').change(function(){
var compid = $(this).val();
if(compid > 0){
//Redirect the user to the page which will show the selected details
location = 'competitions/' + id + '/detailed-registrations?filters=visible&age=all&gender=all';
}
});
});
And then let the Laravel controller method handle the request to the route competitions.detailed-registrations
Source