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

Danny971's avatar

How to handle multiple checkboxes in PHP for storing and processing selections?

I am working on a web application where I have a form with multiple checkboxes, and I'm struggling with how to handle and process the selected checkbox values. I am using PHP and Laravel framework for the backend.

this is a modal with all the available options to choose from

                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h5 class="modal-title">Select Case Referal Type</h5>
                                                    <button type="button" class="close" data-dismiss="modal"><span>&times;</span>
                                                    </button>
                                                </div>
                                                


                                                <div class="modal-body">
                                                <form id="checkboxForm" action="/save-checkboxes" method="POST">
                                                    @csrf
                                                   
                                                   @foreach($referraltype as $type) 
                                                   <input type="hidden" name="family_id" value="{{ $clients->id }}">
                                                <input type="checkbox" name="options[{{ $family_id }}][]" value="{{$type -> id}}" id="checkbox_{{$type->id}}" >
                                                <span style="margin-left: 10px;">{{ $type->referal_description }}</span>
                                                <br>
                                                     @endforeach                        
                                            </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                    <button type="submit" class="btn btn-primary" >Save changes</button>
                                                </div>
                                              </form>
                                            </div>
                                        </div>
                                    </div>


this is my controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Family;
use App\Models\client_profile_ext;
use App\Models\referral;

class savereferraltype extends Controller
{
    public function saveReferralCheckBoxes(Request $request){
        $checkboxes = $request->input('options');

        $family_id = $request->input('family_id');

        $profile = client_profile_ext::where('family_id', $family_id)->first();

        if(!$profile) {
            $profile = new client_profile_ext();
            $profile->family_id = $family_id;
        }
       
        $referralDescriptions = []; 
        
        
        foreach ($checkboxes as $option) {
            $referral = referral::find($option);
            
            if ($referral) {
                $referralDescriptions[] = $referral->referal_description;
            }
        }
        $profile->CaseReferalType = implode(', ', $referralDescriptions);
        $profile->save();
    
        return redirect()->back()->with('success', 'checkbox inserted successfully.');
    }
}


this is my route


  Route::post('/save-checkboxes', [savereferraltype::class,'saveReferralCheckBoxes']) -> name('savereferral');


this is where the results will be displayed after selecting the checkboxess


 <tbody>

                           
                                            <tr>
                                                
                                                <td>Case referal type:</td>
                                                @foreach($crt as $type)
                                                <td> {{$type -> CaseReferalType }}</td>
                                                @endforeach
                                               <td>
                                               @include('modal')
                                                <a href="#" class="edit-link" data-toggle="modal" data-target="#casereferalModal">
                                               
                                                   
                                                   
    <i class="bi bi-pencil-fill bluepen"></i>
</a> </td>
                                                


                                          
                                            </tr>
                            </tbody>
0 likes
3 replies
martinbean's avatar
Level 80

@danny971 If you have checkboxes with the same name, but different values:

<label id="options-{{ $type->id }}">
    <input type="checkbox" name="options[{{ $family_id }}][]" value="{{ $type->id }}" id="options-{{ $type->id }}">
    {{ $type->referral_description }}
</label>

Then when the form is submitted, the checked values will be sent as an array in your controller:

public function someMethod(Request $request)
{
    // Get checked values, grouped by family ID
    $checkedOptions = $request->input('options');
}

You should probably also take care of naming your classes and sticking to naming conventions. Your controller class shouldn’t have an all-lowercase class name for example. It should be StudlyCase, i.e. SaveReferralType instead.

2 likes
Danny971's avatar

@martinbean Thanks for the advice and yeah I did change the name of the controller to SavereferralTypeController.

my mistake was here

<input type="checkbox" name="options[{{ $family_id }}][]" value="{{ $type->id }}" id="options-{{ $type->id }}">

and then i changed it to 

<input type="checkbox" name="options[]" value="{{ $type->id }}" id="options-{{ $type->id }}">
2 likes

Please or to participate in this conversation.