Keep form values after submit

76 views Asked by At

I have a form with dropdown boxes, a radio buttons, and a few text fields. I know how to keep the text field values after form submit, but I would like to keep the dropdown value/data after submit. I am posting to the same page. here is the code below

   <div class="form-group row">
                    <label class="col-sm-3 col-form-label">{{trans('words.shows_text')}}*</label>
                      <div class="col-sm-8">
                            <select class="form-control select2" name="series" id="episode_series_id">
                                <option value="">{{trans('words.select_show')}}</option>
                                @foreach($series_list as $series_data)
                                  <option value="{{$series_data->id}}" @if(isset($episode_info->id) && $series_data->id==$episode_info->episode_series_id) selected @endif>{{$series_data->series_name}}</option>
                                @endforeach
                            </select>
                      </div>
                  </div> 

                  <div class="form-group row">
                    <label class="col-sm-3 col-form-label">{{trans('words.seasons_text')}}*</label>
                      <div class="col-sm-8">
                            <select class="form-control select2" name="season" id="episode_season_id">
                                <option value="">Select Season</option>
                                @if(isset($episode_info->id)) 
                                  @foreach($season_list as $i => $season_data)    
                                      <option value="{{ $season_data->id }}" @if($season_data->id==$episode_info->episode_season_id) selected @endif>{{ $season_data->season_name }}</option>    
                                  @endforeach
                                @endif                                
                            </select>
                      </div>
                  </div>
                    
1

There are 1 answers

2
Methark On

You can use the session variables. So, first start the session:

session_start();

When the form is submitted, store the selected values in session variables:

$_SESSION['radio_value'] = $_POST['radio'];
$_SESSION['checkbox_values'] = $_POST['checkbox'];
$_SESSION['selected_option'] = $_POST['dropdown'];

And finally, use the session variables to set the checked attribute of the appropriate input elements:

<input type="radio" name="radio" value="option1" <?php if ($_SESSION['radio_value'] == 'option1') { echo 'checked'; } ?>>
<input type="checkbox" name="checkbox[]" value="option1" <?php if (in_array('option1', $_SESSION['checkbox_values'])) { echo 'checked'; } ?>>
<select name="dropdown">
  <option value="option1" <?php if ($_SESSION['selected_option'] == "option1") echo "selected"; ?>>Option 1</option>
  <option value="option2" <?php if ($_SESSION['selected_option'] == "option2") echo "selected"; ?>>Option 2</option>
  <option value="option3" <?php if ($_SESSION['selected_option'] == "option3") echo "selected"; ?>>Option 3</option>
</select>

Of course, you will need to make some extra "ifs" in your code.