You just want to send how many zeros are there in the array?
May 7, 2022
20
Level 7
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"
]
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.
Please or to participate in this conversation.