How to I set up the for loop so I can select one radio button per question?

Viewed 34

View of the Question and Radio buttons

Php Laravel radiobutton selects only one radio button for all the given questions. How to I set up the for loop so I can select one radio button per question? I'm using eloquent ORM for setting up relationship between the tables.

                    <div class="card-header">{{$question->question_order}}{{"."}} {{ $question->question }}</div>
                    <div class="card-body">
                        <ul class="list-group">
                            @foreach($question->options as $option)
                                @if(($question->type)=='c')
                                    <label class="container3">
                                        <input type="checkbox" value="{{$option->option_id}}"> {{ $option->option }}
                                        <span class="checkmark"></span>
                                    </label>
                                @elseif(($question->type)=='r')
                                    <label class="container3">
                                        <input type="radio" name="radio" value="{{$option->option_id}}"> {{ $option->option }}
                                        <span class="radio"></span>
                                    </label>                                
                                @endif
                            @endforeach
                            @if(($question->type)=='s')
                                <textarea class="form-control" rows="5" id=""></textarea>
                            @endif
                        </ul>
                    </div>
1 Answers

You need to give unique name option wise to radio group or checkbox, for question wise group and store user response.

Change your for foreach loop as below:

@foreach($question->options as $option)
    @if(($question->type)=='c')
        <label class="container3">
            <input type="checkbox"  name="answer[{{$question->id}}]" value="{{$option->option_id}}"> {{ $option->option }}
            <span class="checkmark"></span>
        </label>
    @elseif(($question->type)=='r')
        <label class="container3">
            <input type="radio" name="answer[{{$question->id}}]" value="{{$option->option_id}}"> {{ $option->option }}
            <span class="radio"></span>
        </label>
    @endif
@endforeach

Here i give name question id wise

Related