How to Keep value of selected option after form submission I make a feature to filter data using the select option. After the user submits to filter data, I want the previously selected values to remain selected.
i using this way for checkbox, but not work for select option.
<option value="mahal" name="mahal" @if (request()->mahal) selected @endif >Harga Tertinggi</option>
my form method is GET.
Hello @subarkah
use like this
$username = $request->old('username');
<input type="text" name="username" value="{{ old('username') }}">
Use old() helper.
<option value="mahal" name="mahal" @if( old('mahal') == request()->mahal) selected="selected" @endif>Foo</option>
<option value="mahal" name="mahal" @if ( old('mahal') == request()->mahal) selected="selected" @endif >Harga Tertinggi</option>
<option value="murah" name="murah" @if ( old('murah') == request()->murah) selected="selected" @endif >Harga Terendah</option>
Your solution is worked, but only option option value="murah" . When i choose option value="mahal" , the option selected still "murah".
A select should only contain one name at the select tag. Not in the options. If you check that structure and fix it. Your code will work perfectly!
This is full code. Still not work perfectly
<select class="form-control" name="urutkan" id="urutkan">
<option value="no" @if( old('urutkan') == request()->no) selected="selected" @endif>Urutkan berdasar</option>
<option value="mahal" @if( old('urutkan') == request()->mahal) selected="selected" @endif>Harga Tertinggi</option>
<option value="murah" @if( old('urutkan') == request()->murah) selected="selected" @endif>Harga Terendah</option>
</select>
<select class="form-control" name="urutkan" id="urutkan">
<option value="no" {{ old('urutkan') == 'no' ? 'selected':'' }}>Urutkan berdasar</option>
<option value="mahal" {{ old('urutkan') == 'mahal' ? 'selected':'' }}>Harga Tertinggi</option>
<option value="murah" {{ old('urutkan') == 'murah' ? 'selected':'' }}>Harga Terendah</option>
</select>
However, I suspect you are breaking one of my rules, NEVER return a view from a POST request, always return a redirect.
I'm using GET request. should i use redirect ?
I solved with this
<select class="form-control" name="urutkan" id="urutkan">
<option value="no" @if(request()->urutkan == 'no') selected @endif>Urutkan berdasar</option>
<option value="mahal" @if(request()->urutkan == 'mahal') selected @endif>Harga Tertinggi</option>
<option value="murah" @if(request()->urutkan == 'murah') selected @endif>Harga Terendah</option>
</select>
@subarkah
you should follow the @snapey answer because that one is the best practice
Please sign in or create an account to participate in this conversation.