Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

subarkah's avatar

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.

0 likes
9 replies
a4ashraf's avatar

Hello @subarkah

use like this

$username = $request->old('username');

<input type="text" name="username" value="{{ old('username') }}">



SarwarAhmed's avatar

Use old() helper.

<option value="mahal" name="mahal" @if( old('mahal')  == request()->mahal) selected="selected" @endif>Foo</option>
1 like
subarkah's avatar

<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".

Kumaravel's avatar

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!

subarkah's avatar

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>
Snapey's avatar
<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.

1 like
subarkah's avatar

I'm using GET request. should i use redirect ?

subarkah's avatar

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>

Please or to participate in this conversation.