laravel 在laravel 5.2中提交表单后如何在文本字段中保留值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44601007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to keep values in textfields after submitting a form in laravel 5.2
提问by Dbb
I have a form to filter data as per the entered from and to date. Everything is working fine but I just want that values should be keep in textfields.
I tried as -
view -
我有一个表格可以根据输入的日期和日期过滤数据。一切正常,但我只是希望值应该保留在文本字段中。
我试过 -
查看 -
<form action="{{url('user/manage')}}" method="post">
<input type="text" placeholder="From" name="from_date" value="{{old('from_date')}}" id="from_date" class="form-control">
<input type="text" placeholder="To" name="to_date" value="{{old('to_date')}}" id="to_date" class="form-control">
<input type="submit" value="Filter" name="submit" class="btn btn-default">
<input type="text" placeholder="Enter E mail" name="email_search" id="" value="{{old('email_search')}}" class="form-control">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<input type="submit" value="Show" name="submit" class="btn btn-default">
</form>
Controller -
控制器 -
$values=[];
$values['from_date']=$from;
$values['to_date']=$to;
return view('show',['users'=>$users])->withInput($values);
Please Help me to solve this.
Thanks.
请帮我解决这个问题。
谢谢。
回答by
Input data is stored in the session between requests. Are you redirecting after a request has processed, or are you trying to maintain inputs in the case a request is not validated, i.e. redirected back to the page with error messages?
输入数据存储在请求之间的会话中。您是在请求处理后重定向,还是在请求未验证的情况下尝试维护输入,即重定向回带有错误消息的页面?
In the first case you can use $request->session()->flash()
to keep the data in the session after the request has processed.
在第一种情况下,您可以$request->session()->flash()
在请求处理后将数据保留在会话中。
In the second case, data is flashed automatically and accessible through the old()
helper method, as you have in your form example.
在第二种情况下,数据会自动刷新并通过old()
辅助方法访问,就像您在表单示例中所做的那样。
回答by Shailesh Ladumor
try this. your form and controller method should be like
尝试这个。你的表单和控制器方法应该像
// form
//表格
<form action="{{url('user/manage')}}" method="post">
<input type="text" placeholder="From" name="from_date" value="{{$from_date}}" id="from_date" class="form-control">
<input type="text" placeholder="To" name="to_date" value="{{$to_date}}" id="to_date" class="form-control">
<input type="submit" value="Filter" name="submit" class="btn btn-default">
<input type="text" placeholder="Enter E mail" name="email_search" id="" value="{{old('email_search')}}" class="form-control">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<input type="submit" value="Show" name="submit" class="btn btn-default">
</form>
// controller
//控制器
$data = [
'users'=>$users,
'from_date'=>$from,
'to_date'=>$to; ]
return view('show',$data);
]