Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gust's avatar
Level 1

How to make and access this from $request

I want to generate(using form helpers if possible) a question(text field) and 4 choices(text fields, each of these with correct radio button)as many times as the amount of given a variable $questions that was passed into this view via route and be able to access these form variables via loop from my $request and then access each question(text) and choices(text) and if the current choice being checked has the radio button checked to save it as 1 if not as 0 and save it into my DB which has a Table/Model Question and Table/Model Choice

Here is my latest attempt

     <form action="{{route('quiz.storeQuestions')}}" method="POST">
                    {{csrf_field()}}
                    @for($i = 0; $i < $questions;$i++)
                        <input type="text" name="question[]" value="">

                        <br>
                        @for($j = 0;$j < 4;$j++)
                            Choose <input type="checkbox" name="isCorrect[{{$i}}][]" value="0" onchange="check()">
                            <input type="text" name="choice[]">
                            <br>
                        @endfor
                        <br>
                    @endfor
                        <br>
                        <input type="submit">
                </form>

Problems are

  1. the checkbox regardless if being checked or not has the value of 0(original value)
  2. If a checkbox is not clicked it doesn't exist in the array isCorrect[{{$i}}][]
  3. Multiple checkboxes in the same set of 4 questions can be clicked instead of just 1

I think the behavior I am(or should be) going for is a multidimensional array of isCorrect[][] whose first dimension is the number of questions and each array within holds the values of correct(1) and incorrect(0) depending on if the checkbox is checked [ [0,1,0,0], [1,0,0,0], .... [0,1,0,0], ]

tho not that good at it,I have tried using JS but its not working getting Uncaught ReferenceError: check is not defined at HTMLInputElement.onchange (VM10445 16:78)

    window.onload = function () {



    function check() {
        var input = document.querySelector('input[type=checkbox]');
        var a = input.checked ? "checked" : "not checked";
        input.value = 1;
    }

    input.onchange = check;
    check();

    // $("input[type='checkbox']").change(function () {
    //     if($(this).is(":checked")){
    //         $(this).value = 1;
    //     }
    // })
}

Anyone ideas?

0 likes
0 replies

Please or to participate in this conversation.