Home > Enterprise >  How do I keep checkbox checked after refresh
How do I keep checkbox checked after refresh

Time:02-22

0

am trying to check checkbox after i search categories. but its errors explode(): Argument #2 ($string) must be of type string, array given

 <input id="subCats-{{$s->id}}" name="category[]" type="checkbox" value="{{ $s->id }}"   
@if (request()->category != "" && in_array($s->id,explode(',', request()->category)))
checked
@endif
>

and am having problem with

     <input type="checkbox" name="level" value="0" id="lvl-0"
                                @if (request()->level == 0)
                            checked
                            @endif

it's always is checked because if i don't requests its null and it's thinks that its 0 i tried (request()->level === 0) but it's still don't works.

others with level are working

     <input type="checkbox" name="level" value="1" id="lvl-1"
                                @if (request()->level == 1)
                            checked
                            @endif
     <input type="checkbox" name="level" value="2" id="lvl-2"
                                @if (request()->level == 2)
                            checked
                            @endif

CodePudding user response:

The problem is your

in_array($s->id,explode(',', request()->category)))

explode generates an array from a string, but your "request()->category might not give you a string, it is already an array.

so maybe its working with this:

in_array($s->id, request()->category ?? []))

CodePudding user response:

I think it's being checked regardless of the if statement. So this might work:

<input type="checkbox" :checked="{{ request()->level === 0 }}" />
  • Related