user3718908x100
user3718908x100

Reputation: 8509

Radio buttons with multi-dimensional array

Hello i have the following form in laravel:

<?php $i = 0; ?>
        @foreach ($quiz as $q)
        <div class="row">
            <div class="col-md-12">

                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">{{ '<b>' . ($i + 1). '.</b>  ' . $q->question }}</h3>
                    </div>
                    <div class="panel-body">
                        <?php
                            $a = json_decode($q->answers);
                        ?>

                        <div class="form-group col-md-6">
                            <label class="radio inline" for="correctAnswer[0]">
                                {{ Form::radio('correctAnswer[$i][0]', 'A', array('class' => 'iradio', 'required' => 'required')) }}
                                {{ $a[0] }}
                            </label>
                        </div>

                        <div class="form-group col-md-6">
                            <label class="radio inline" for="correctAnswer[0]">
                                {{ Form::radio('correctAnswer[$i][1]', 'B', array('class' => 'iradio', 'required' => 'required')) }}
                                {{ $a[1] }}
                            </label>
                        </div>


                        <div class="form-group col-md-6">
                            <label class="radio inline" for="correctAnswer[0]">
                                {{ Form::radio('correctAnswer[$i][2]', 'C', array('class' => 'iradio', 'required' => 'required')) }}
                                {{ $a[2] }}
                            </label>
                        </div>


                        <div class="form-group col-md-6">
                            <label class="radio inline" for="correctAnswer[0]">
                                {{ Form::radio('correctAnswer[$i][3]', 'D', array('class' => 'iradio', 'required' => 'required')) }}
                                {{ $a[3] }}
                            </label>
                        </div>
                    </div>
                </div>

            </div>
        </div>
        <?php $i++; ?>
        @endforeach

As you may have noticed i am using a multi dimensional array as the name for the radio inputs eg.:

{{ Form::radio('correctAnswer[$i][0]', 'A', array('class' => 'iradio', 'required' => 'required')) }}

Each question set has four possible answers and the user is required to pick one, however because of the multidimensional array more than 1 input can be selected even though they are a radio group: enter image description here

I would like to know how i can fix this, thank you in advance. :)

Upvotes: 0

Views: 532

Answers (1)

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

When using radio buttons, you need to set same name for a group of radio buttons. But you set different name for them. You must change all like below(Just remove the [0],[1],[2],...):

{{ Form::radio('correctAnswer[$i]', 'A', array('class' => 'iradio', 'required' => 'required')) }}
<!-- Rest of code -->

Upvotes: 1

Related Questions