从 Laravel 5 中的复选框获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41372404/
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
Getting values from checkboxes in Laravel 5
提问by sogeniusio
I'm working on a quote request form on my site and need some help with functionality. I'm trying to get the values from a set of checkboxes that I passed to the view; like so:
我正在我的网站上处理报价请求表,需要一些功能方面的帮助。我正在尝试从传递给视图的一组复选框中获取值;像这样:
Controller (getQuote)
控制器 (getQuote)
public function getQuote()
{
$services = array(
'Accept donations',
'Update website on your own',
'E-Commerce',
...
);
return view('pages.quote')->withServices($services);
}
Controller (postQuote)
控制器(引用后)
public function postQuote(Request $request)
{
$this->validate($request, [
'services' => 'required|array',
...
]);
$data = array(
'services' => $request->service,
...
);
$data['services_checked'] = Input::get('service');
if(is_array($services_checked))
{
}
Mail::send('emails.quote', $data, function($message) use ($data){
$message->from($data['email']);
$message->to('[email protected]');
$message->subject('New quote request for ' . $data['firstname'] . ' ' . $data['lastname']);
});
return redirect()->back()->with('message', "Thanks, your quote request has been sent");
}
View
看法
I then loop through the values in the passed array and display it to the user.
然后我遍历传递的数组中的值并将其显示给用户。
<article class="col-md-12 text-left services bx1-margin">
{{ Form::label('services', "Please check all the functionality you would like to see in your new website:") }}
{{-- <label for="services">Please check all the functionality you would like to see in your new website.</label> --}}
<ul>
@foreach ($services as $servicesid => $service)
<li>
{{-- {{ Form::checkbox('agree', 1, true) }} --}}
<input id="{{$servicesid}}" value="{{$servicesid}}" name="service[{{$servicesid}}]" type="checkbox" class="custom-control-input form-check-input">
<span class="custom-control-description">{{$service}}</span>
{{-- <input id="s-{{$servicesid}}" name="services" type="checkbox" class="custom-control-input form-check-input"> --}}
</li>
@endforeach
</ul>
</article>
When the user submits the form, how do I go about receiving the checked checkboxes?
当用户提交表单时,我该如何接收选中的复选框?
Research
研究
I saw this post here:How to get the values for a series of checkboxes in Laravel 4 controller (if checked)I saw a post her (stackoverflow)and it explains a setting the value to array[]
, I'm just not quite sure about implementation.
我看到这个帖子在这里:如何获得一系列的复选框的值Laravel 4控制器(如果选中)我看到一个帖子她(计算器),它解释了设定值array[]
,我只是不太确定有关实施.
回答by xmhafiz
Make sure the input
for type checkbox
using the same name as services[]
in your html form
确保input
for 类型checkbox
使用与services[]
html 表单中相同的名称
<input id="{{$servicesid}}" value="{{$servicesid}}" name="service[]" type="checkbox" class="custom-control-input form-check-input">
So when you submit, just check the variable $request->service
in your postQuote
method. It should contains the array variable with all checked inputs submitted.
所以,当你提交,只是检查变量$request->service
在你的postQuote
方法。它应该包含已提交所有检查输入的数组变量。