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

TimiAde's avatar

How Can I Send Only The Hidden Value When The Checkbox is not checked

I am building a quiz app and only want to send the value 0 when the checkbox is not checked. How can i go about it.

<form action="{{route('result')}}" method="post">
            @csrf
            @foreach ($questions as $question)

            <div>
                <p>{{$loop->index + 1}} {{ $question->the_question }}</p>
                    <input type="hidden" name="answer[]" value=0>
                <ul>
                    @foreach ($question->options as $option)
                    @php
                        $i = $loop->index;
                    @endphp
                    <li>
                        <input type="checkbox" name="answer[]" value="{{$i + 1}}">
                    {{$option->the_option}}
                    </li>
                    @endforeach
                </ul>
            </div> 

            @endforeach
            <button type="submit" name="submit">Submit</button>
        </form>

i have five questions. when i check all answers i get this array

^ array:10 [▼
  0 => "0"
  1 => "3"
  2 => "0"
  3 => "1"
  4 => "0"
  5 => "4"
  6 => "0"
  7 => "1"
  8 => "0"
  9 => "1"
]
0 likes
20 replies
TimiAde's avatar

@aurelianspodarec i only want to send five values in the range of 1-4. but the hidden value sends whether or not i check the checkbox

aurelianspodarec's avatar

@TimiAde I might be misunderstanding.

What are the five values in the range of 1-4? Do you mean you want to send only values only if they are: 1,2,3,4?

So this is your array:

^ array:10 [▼
  0 => "0"
  1 => "3"
  2 => "0"
  3 => "1"
  4 => "0"
  5 => "4"
  6 => "0"
  7 => "1"
  8 => "0"
  9 => "1"
]

and you want this?

^ array:10 [▼
  0 => "3"
  1 => "1"
  2 => "4"
  3 => "1"
  4 => "1"
]

Oh, I guess the 5 selected values in the array.

I guess one way to do this would be to delete all values that have number 0 in them, before sending the array I guess.

MohamedTammam's avatar

Don't send it, and if you don't get a value in the back-end suppose it's 0.

Snapey's avatar
                    <li>
						<input type="hidden" value="0" name="answer[{{$i + 1}}]" />
                        <input type="checkbox" name="answer[{{$i}}]" value="{{$i}}">
                    {{$option->the_option}}
                    </li>

hidden value will be sent if checkbox is not checked

2 likes
TimiAde's avatar

@Snapey the options are four

<li>
                        <input type="checkbox" name="answer[]" value="{{$i + 1}}">
                    {{$option->the_option}}
                 </li>
<li>
                        <input type="checkbox" name="answer[]" value="{{$i + 1}}">
                    {{$option->the_option}}
                    </li>
<li>
                        <input type="checkbox" name="answer[]" value="{{$i + 1}}">
                    {{$option->the_option}}
                    </li>
<li>
                        <input type="checkbox" name="answer[]" value="{{$i + 1}}">
                    {{$option->the_option}}
                    </li>
martinbean's avatar

@TimiAde Approach will still work. Put a hidden input with the same name but a value of 0 before any of the checkboxes. If no checkboxes are checked, then the hidden input’s value of 0 is submitted to the server instead.

TimiAde's avatar

@martinbean i used a foreach

@foreach ($question->options as $option)
                    @php
                        $i = $loop->index;
                    @endphp
                    <li>
                        <input type="checkbox" name="answer[]" value="{{$i + 1}}">
                    {{$option->the_option}}
                    </li>
                    @endforeach
jlrdw's avatar

@TimiAde you are not concerned with old data? And you can loop in controller to find which values were checked, but The hidden with value="0" works also, I just don't use it any longer.

Snapey's avatar

what was wrong with the code I gave?

TimiAde's avatar

thanks @aurelianspodarec @mohamedtammam @snapey @martinbean @jlrdw i didnt use hidden again. just sent the key value pair to the controller and fill the empty keys with value 0.

<form action="{{route('result')}}" method="post">
            @csrf
            @foreach ($questions as $question)
            @php
                $j = $loop->index;
            @endphp
            <div>
                <p>{{$loop->index + 1}} {{ $question->the_question }}</p>
                <ul>
                    @foreach ($question->options as $option)
                    @php
                        $i = $loop->index;
                    @endphp
                    <li>
                        <input type="checkbox" name="answer[{{$j}}]" value="{{$i + 1}}">
                    {{$option->the_option}}
                    </li>
                    @endforeach
                </ul>
            </div> 

            @endforeach
            <button type="submit" name="submit">Submit</button>
        </form>

the ResultsController

 public function getResults(Request $request)
    {
        $array = $request->answer;
        for($i =0; $i<5; $i++){
            if(Arr::exists($array, $i) == false){
                $array[$i] = "0";
            }
        }
        ksort($array);
        dd($array);
    }
MohamedTammam's avatar

@TimiAde Glad to hear that, however I don't think that the best way. But you know better.

please close the discussion.

aurelianspodarec's avatar

@MohamedTammam How do you close a discussion?

Well, I think when people learn they understand their way of doing things. Sometimes doing a different solution might be too much so its not always the best to just go with someone else solution right away if you're still learning. Not that he knows better its just how one learns.

MohamedTammam's avatar

@aurelianspodarec You close it but choosing the best answer.

When people learn new things it's good to know different opinions in doing stuff, that way they gain more knowledge/experience.

1 like
MichalOravec's avatar
Level 75

@TimiAde You can remove @php directive from your blade file, it could be like this:

<form action="{{ route('result') }}" method="POST">
    @csrf

    @foreach ($questions as $question)
        <div>
            <p>
                {{ $loop->iteration }} {{ $question->the_question }}
            </p>

            <ul>
                @foreach ($question->options as $option)
                    <li>
                        <input type="checkbox" name="answer[{{ $loop->parent->index }}]" value="{{ $loop->iteration }}">

                        {{ $option->the_option }}
                    </li>
                @endforeach
            </ul>
        </div>
    @endforeach

    <button type="submit">
        Submit
    </button>
</form>

https://laravel.com/docs/9.x/blade#the-loop-variable

I didn't focus on your problem, I just fixed your code.

aurelianspodarec's avatar

@MohamedTammam Oh, I see what you mean. The thread stays open after you select the best answer but it doesn't show in unanswered yeah, fair enough.

Yeah, 100%. I always... well I always ask "How would you do it as a senior dev" - but that gets mixes answers :D

I think seeing how other people do it gives you exposure and skyrockets ones learning. As long as you go back to the answer and use it later and read what they have done and not ignore it - not saying he did, just saying in general :D

Snapey's avatar

whatever

Its obvious why your original code does not give you what you wanted, but you don't want to learn that apparently.

Please or to participate in this conversation.