我有一个搜索表单,但在提交时它不会显示之前选择的选项。
这是我的表单:
<form role="form" class="advanced-search-form" method="post" action="{{ url('/searchresults') }}">
{{ csrf_field() }}
<div class="row">
<div class="col-md-4 form-group">
<label for="exampleSelect1">Select city: </label>
<select class="form-control" id="exampleSelect1" name="selectcity">
@if (count($cities) > 0)
@foreach ($cities as $city)
<option value="{{$city->id}}" @if( old('selectcity') == $city->id) selected="selected" @endif>{{$city->name}}</option>
@endforeach
@endif
</select>
</div>
<div class="row">
<div class="form-group col-md-4 col-md-offset-8">
<button class="btn btn-light-blue-2 pull-right" type="submit">Search</button>
</div>
</div>
</form>
这是我的路线: Route::post('/searchresults', 'SearchController@index');
我的控制器中的操作
public function index()
{
$cities = DB::table('cities')
->select('cities.name', 'cities.id')
->orderBy('name', 'ASC')
->get();
return view('pages.searchresults', compact('cities'));
}
目前我没有显示结果,我首先需要解决为什么表单在提交后清空,并且不返回旧值并设置所选选项。
答案 0 :(得分:3)
正如文件https://laravel.com/docs/5.3/requests
中所写的那样Illuminate \ Http \ Request类上的flash方法将闪烁当前对会话的输入,以便在用户下次向应用程序发出请求时可用:
$request->flash();
你应该使用像这样的重定向
return redirect('yourTarget')->withInput();