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

Kaustubh's avatar

How to pass Checkbox value 0 if not checked and 1 if checked using array.

Here its my HTML form

<form action="{{ url('NewPer') }}" method="post" class="formPermission">
{{ csrf_field() }}

@foreach($perList2 as $li)
    <div class="row">
      <div class="col-sm-4">{{ $li->p_Oname }}</div>
      <div class="col-sm-2"><input type="hidden" name="view[]" value="off"><input name="view[]" type="checkbox" value="on"></div>
      <div class="col-sm-2"><input type="hidden" name="add[]" value="off"><input name="add[]" type="checkbox" value="on"></div>
      <div class="col-sm-2"><input type="hidden" name="update[]" value="off"><input name="update[]" type="checkbox" value="on"></div>
      <div class="col-sm-2"><input type="hidden" name="delete[]" value="off"><input name="delete[]" type="checkbox" value="on"></div>
      
    </div><br>
@endforeach
    <div><input type="submit" class="btn btn-success pull-left" name="submit" value="Save"></div>
</form>

After Post It is taking hidden input if checkbox is checked

array:8 [▼
  "_token" => "2jLsqtIk80XlMyvcO0FrzLW4NqtTQDtywQ9bnoNe"
  "view" => array:6 [▼
    0 => "off"   ------------------->Hidden Input
    1 => "on"
    2 => "off"   ------------------->Hidden Input
    3 => "on"
    4 => "off"   ------------------->Hidden Input
    5 => "on"
  ]
  "add" => array:3 [▶]
  "update" => array:3 [▶]
  "delete" => array:3 [▶]
  "submit" => "Save"
]
0 likes
11 replies
Snapey's avatar

What's the question? 1 and 0 instead of on and off? Just change the values...

1 like
mushood's avatar

@Kaustubh

This might help:

If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.

There are two ways to fix this easily:

  1. Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn't always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won't work.

  2. Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

Note: If your names are in an array form (ie they have square brackets in them), this won't work, as the hidden fields will increment the array count as well.

Source: https://stackoverflow.com/questions/14067215/unchecked-checkbox-returning-null-value

so since you are using option two, you can't use the []

change your code to this:

@foreach($perList2 as $li)
    <div class="row">
      <div class="col-sm-4">{{ $li->p_Oname }}</div>
      <div class="col-sm-2"><input type="hidden" name="view" value="off"><input name="view" type="checkbox" value="on"></div>
      <div class="col-sm-2"><input type="hidden" name="add" value="off"><input name="add" type="checkbox" value="on"></div>
      <div class="col-sm-2"><input type="hidden" name="update" value="off"><input name="update" type="checkbox" value="on"></div>
      <div class="col-sm-2"><input type="hidden" name="delete" value="off"><input name="delete" type="checkbox" value="on"></div>
      
    </div><br>
@endforeach

if you check your input named "view", you should get the correct output for checked/unchecked.

2 likes
Cronix's avatar

<input name="delete" type="checkbox" value="on">

$checked = $request->has('delete') ? 1 : 0;

Just check whether it passed anything for that field. If it did, it was checked and assign the value of 1, if it didn't it wasn't and assign value of 0. It doesn't matter if you use 1/0 or on/off or yes/no or anything else for your actual values. If the field exists in the request it means it was checked because html doesn't send unchecked values.

3 likes
Snapey's avatar
Snapey
Best Answer
Level 122

If you want to go the hidden field route, add an iterator to the loop

<form action="{{ url('NewPer') }}" method="post" class="formPermission">
{{ csrf_field() }}

@foreach($perList2 as $li)
   <?php $i=$loop->index; ?>
    <div class="row">
      <div class="col-sm-4">{{ $li->p_Oname }}</div>
      <div class="col-sm-2"><input type="hidden" name="view[{{$i}}]" value=0><input name="view[{{$i}}]" type="checkbox" value=1></div>
      <div class="col-sm-2"><input type="hidden" name="add[{{$i}}]" value=0><input name="add[{{$i}}]" type="checkbox" value=1></div>
      <div class="col-sm-2"><input type="hidden" name="update[{{$i}}]" value=0><input name="update[{{$i}}]" type="checkbox" value=1></div>
      <div class="col-sm-2"><input type="hidden" name="delete[{{$i}}]" value=0><input name="delete[{{$i}}]" type="checkbox" value=1></div>
      
    </div><br>
@endforeach
    <div><input type="submit" class="btn btn-success pull-left" name="submit" value="Save"></div>
</form>

6 likes
jlrdw's avatar

If not checked, nothing is passed: SO

$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0');

something like that.

1 like
jlrdw's avatar

So much easier not to mess with hidden, just deal with a 0 or 1.

  • 0 if nothing was passed
  • 1 of course if passed.

But really all this over a simple checkbox.

Sorry missed @Cronix answer, where it's already explained.

1 like
Snapey's avatar

Well not quite that simple because the checkbox is in an array of checkboxes so the same name occurs multiple times. The problem in the OP is that the hidden field and the checkbox field use [] so both the hidden field and the checkbox end up being added to the array instead of just one.

jlrdw's avatar

I don't like the array approach, I like just dealing with individual checkbox data, one at a time - just habit.

Please or to participate in this conversation.