N
N
nevesomostjke2022-02-21 07:16:34
HTML
nevesomostjke, 2022-02-21 07:16:34

How to display checkboxes in foreach in selection order?

I make the appointment of routes for drivers.
Here is the code:

@foreach ($storages as $storage)
<input type="checkbox" name="stock[{{ $storage->name }}]" class="custom-control-input">
@endforeach

In controller:
foreach ($request->stock as $k=>$v) {
            echo $k;
        }


How to print the value of $k in checkbox selection order?
Those. Let's say I have 2 lines. I click the checkboxes starting from the top and in the foreach output I see:
$k[0] and $k[1]
And if I click the checkboxes from bottom to top, I see the same thing. How can I make foreach print in the order in which checkboxes are selected?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-02-21
@nevesomostjke

In the case of a regular form, the browser does not send information about the order in which the checkboxes are selected.
To do what you want, you need to fence on the Javascript front, which will keep track of the order of "clicking" on the checkboxes and somehow pass this information to the server. I will assume that in your case this means a significant reworking of the system.
Whether this "whistle" is worth such an effort is up to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question