I deduce answers to questions using pagination (1 question per page). I want the old input of the checkbox to remain when switching from page to page. I looked at all the questions on the stack on this topic, but nothing helped.

@foreach ($quest->answer as $answer) <div class="custom-control custom-checkbox ml-5 my-1"> <input type="checkbox" name="checkbox[{{ $answer->id }}]" value="{{ $answer->id }}" @if (is_array(old('checkbox')) && in_array($answer->id, array_keys(old('checkbox')))) checked @endif> <label class="custom-control-label h6-responsive" for="checkbox-answer{{ $answer->id }}">{{ $answer->answer }}</label> </div> @endforeach {{ $questions->links() }} 

enter image description here

  • And how do you consider the answers: some kind of js, or server-side calculations? - Rustam Gimranov
  • I have not written it yet, but I plan that the entire pagination output will be in one form and the button will be sent to the server for verification and recording, and then a redirect back. - BorsheC 3:08 pm
  • Added a screenshot for clarity. Before being sent to the server, the user can move between questions and, accordingly, the selected checkboxes should be saved when moving. I cannot figure out how to do this yet ... I tried to display the entire array on the page and cut it into pages in jquery, but it did not work either ( - BorsheC
  • The simplest thing that comes to mind is to place instead of pagination a bunch of submit <input type="submit" name="question" value="ТУТ НОМЕР ВОПРОСА"> - Rustam Gimranov
  • You must first on the server side to organize the logic of calculations and determine the relationship of models User Question Answer. If there is one, then it is better to attach to the question. - Rustam Gimranov

1 answer 1

Markup did not save. The checkbox name specifically set another other_checkbox . The checkbox is displayed like this, in your case, if it is an array of values

 <label> <input type="checkbox" name="other_checkbox[888]" value="1" @if(old('other_checkbox.888', 0) == 1) checked @endif /> Ответ 888</label> <label> <input type="checkbox" name="other_checkbox[999]" value="1" @if(old('other_checkbox.999', 0) == 1) checked @endif /> Ответ 999</label> 

This is how validation is performed.

 $this->validate($request, [ 'other_checkbox' => 'sometimes|required|array', 'other_checkbox.*' => 'required|accepted' ]); 
  • This is all fine, but the problem is that it will only work when you click on the button to complete (one button for all questions (for example, 10 pagination pages)), but it is necessary that when you switch from page 1 to page 2, the selected checkbox on page 1 remains. it became clearer to say that this is a user testing system. Or maybe there is a plugin for the implementation of testing? - BorsheC
  • Surely there are already ready-made packages, look at the githaba. - Rustam Gimranov