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

skoobi's avatar
Level 13

Saving checkboxes to seperate db row

Hi i'm really, really struggling to get my project that saves checkboxes to work. I know some basic stuff with arrays, but this is kicking my a*s...

** What I'm trying to do is:: **

I have a list of activities for a dog, there could be up to 3 dogs and i have the template do a foreach loop through each dog and display a checkbox for that dog.

So if there was 2 dogs, the checkboxes would be like so:

Activity 1

  • Prebook Molly
  • Prebook Polly

Activity 2

  • Prebook Molly
  • Prebook Polly

etc....

You can check either one and it saves to the database.

The issue i'm getting is if i want to do it as a relationship, I cant get it to go through a foreach if theres an offset. i.e. if molly gets checked for the first activity then it is ok but if just Polly gets checked then it will fail with undefined offset due to it being [1].

The other way i tried was to serialise the array. This works but as soon as i try to display all the items in the admin it wont allow me to unserialise in the @foreach loop.

Has anybody got a solution or an idea on what the best way to do this is?

Many thanks

0 likes
3 replies
austenc's avatar

Hi there! Array-based checkbox fields can be a bit of a struggle sometimes because of most browsers' default behavior. If a box isn't checked, the browser won't even send that index in the next request.

Anyway, there are two common strategies you'll see:

  • Giving the checkbox fields each a unique key like <input type="checkbox" name="dogs[0]" /> (in this case 0 would be something unique to each one like a dog ID or something
  • Giving checkbox fields a value like <input type="checkbox" name="dogs[]" value="0" /> (again,0` would be your dog ID or whatever here)
skoobi's avatar
Level 13

Hi thank you for the reply.

Ah thats why its not working. I have added

<input type="checkbox" name="activity1[]" value="{{$dog->id}}" /> in the view

and then the controller serialises it but cant get it to work at all.

I think i'm going to have to try a different method.

Any suggestions on how this can be done?

If i got

Activity 1

  • dog 1
  • dog 2

Activity 2

  • dog 1
  • dog 2

etc.

mvd's avatar

You can use a multidimensional array, something like this (only a example in plain php)

$activities = array('activity 1', 'activity 2', 'activity 3');
$dogs = array('dog 1', 'dog 2');


// Check for post
if (isset($_POST['activity'])) {
    print '<pre>';
    var_dump($_POST['activity']);
}


// Create the form.
print '<form method="post">';

// Loop over the activities.
foreach ($activities as $activityId => $activity) {
    print '<h2>' . $activity . '</h2>';

    print '<ul>';
    // Loop over the dogs
    foreach ($dogs as $dogId => $dog) {
        print '<li><input type="checkbox" name="activity[' . $activityId. '][' . $dogId . ']"> ' . $dog . '</li>';
    }
    print '</ul>';
}

print '<input type="submit" value="submit" /></form>';

If the form is posted, you can loop over $_POST['activity'] to check the selected activities for a dog.

Please or to participate in this conversation.