1
我有一个搜索表单,但它在提交时不显示之前选定的选项。提交laravel后搜索表单不显示旧值5.3
这里是我的形式:
<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', '[email protected]');
在我的控制器动作
public function index()
{
$cities = DB::table('cities')
->select('cities.name', 'cities.id')
->orderBy('name', 'ASC')
->get();
return view('pages.searchresults', compact('cities'));
}
目前我没有显示结果,我首先需要解决为什么表单在提交后清空,并且不返回旧值并设置选定选项。
重定向做岗位方法而不是返回查看重定向。 –
首先:当处理表单并希望填充它们时,您应该查看https://laravelcollective.com/docs/5.0/html。 其次 - 是否有一个原因,你为什么不使用城市模型,而不是原始查询?第三 - 重定向回形式并保留数据,你可以使用redirect() - > back() - > withData(),还包括 - > with(compact('cities')) – larsemil
['old()'] (https://laravel.com/docs/5.4/helpers#method-old)只有在按照文档将旧输入数据刷新到会话时才有效。如果你想记住这些数据,你将不得不手动刷新它,或将选定的值发送回你的页面。 – Jerodev