How to get selected radio button value in Laravel
Asked Answered
S

2

6

Hi all how to get selected radio button value. I want to store values on my table where selected radio button.

My Controller code passing to View $result

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

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

In my view code I had radio button.

{{ 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() }}         

So I try to pass chechked radio button value to my postValue function when I click Add button.

Surface answered 27/4, 2014 at 8:50 Comment(0)
H
9

You need to give the radio button a value. All you've given it is a name.

<td>{{ Form::radio('result', $result->code) }}

Then in your controller that processes the form you can grab the value.

$result = Input::get('result');
Higher answered 27/4, 2014 at 9:40 Comment(3)
Nice. Your code is working. But i had lot of value. I can put them array?Surface
If you want to be able to select multiple you should be using a checkbox and the name would be result[]. Then Input::get('result'); would contain an array of values.Higher
No Checkbox is no need. I can select only one value. This one value is have multiple values inside. For example name, code, age, date time etc. You can see example image following link linkSurface
T
3

You need to give a name to the radio button.

Normal HTML : <input name="sex" type="radio" value="male">

Laravel : {{ Form::radio('sex', 'male') }}

Form::radio('name','value');

Laravel gets parameter for name and value.

Teratology answered 10/9, 2014 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.