如何在Laravel中获取选定的单选按钮值

6

大家好,如何获取选定的单选按钮值。我想将所选单选按钮的值存储在我的表格中。

我的控制器代码传递给视图 $result

        $result =     DB::table('table')
                                ->where('name', $name)
                                ->where('code', $code)
                                ->get();

        return  View::make('home.search_result')            
                       ->with('result', $result);

在我的代码中,我有一个单选按钮。
{{ Form::open(array('action' => 'BookingController@postValue',  'class'=>'well', 'method' => 'GET')) }}         
<table id="search" class="table table-striped table-hover">
    <thead>
        <tr>
            <th></th>
            <th>Name</th>
            <th>Code</th>                                                                                                              
        </tr>
    </thead>
    <tbody> 
        @foreach($result as $result)                                    
            <tr class="success">    
                <td>{{ Form::radio('result') }}
                <td>{{ $result->name}}</td>                 
                <td>{{ $result->code}}</td>                 
            </tr>                       
        @endforeach                           
    </tfoot>
</table>           

{{ Form::submit('Add', array('class' => 'btn btn-info')) }}         
{{ Form::close() }}         

当我点击“添加”按钮时,我尝试将选中的单选框的值传递给我的“postValue”函数。

2个回答

9
您需要为单选按钮设置一个值。您只给它了一个名称。
<td>{{ Form::radio('result', $result->code) }}

然后在处理表单的控制器中,您可以获取该值。

$result = Input::get('result');

不错,你的代码运行正常。但是我有很多值,我可以把它们放到数组里吗? - JaN
如果您想要能够选择多个选项, 您应该使用复选框,名称将是result[]。然后 Input::get('result'); 将包含一个值数组。 - Jason Lewis
不需要复选框。我只能选择一个值。这个值里面有多个值,比如姓名、代码、年龄、日期时间等。你可以在以下链接中看到示例图片link - JaN

3
您需要给单选按钮一个名称。
普通HTML:<input name="sex" type="radio" value="male"> Laravel:{{ Form::radio('sex', 'male') }}
Form::radio('name','value');

Laravel可以获取名称和值参数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接