Neal23
Neal23

Reputation: 79

Save checkbox values into database

I am designing a form where the user can fill up their particulars and submit it to the database. For the most part, it works. There is one part with the form about the driving license class, it does not work. A person can have multiple driving licenses so I made that into a checkbox field. However, when the user selects two or more driving licenses, only the lastly selected driving license appears in the database. So, if a person selects Class1 firstly and then selects Class2, only Class2 appears in the database.

How can I include all the driving licenses selected by the user into one database column? I have attached the code below.

form.blade.php

@extends('layout.basiclayout')
@section('content')

<p>page 1/7</p>
        {!! Form::open(['url' => 'form/submit']) !!}

             <h3 style= 'text-decoration: underline'>PERSONAL PARTICULARS - </h3>
             <div class="form-group">
                  {{Form::label('Name/NRIC', 'Name AS PER NRIC/PASSPORT: ')}}
                  {{Form::text('Name/NRIC', '', ['class' => 'form-control', 'placeholder' => 'eg: John Smith'])}}
            </div>

            {{-- start of Driver`s License --}}
            <p>
            <div class="editfield">
              <div class="radio">
                <span><b>Do you have a Driver`s license?</b></span>
                <div id="Driver_licenseID">
                   <label><input type="radio" name="Driver_license" id="yesid" value="Yes" onclick="document.getElementById('Driver_license').style.display='block'">Yes</label>
                  <label><input type="radio" name="Driver_license" id="noid" value="No" onclick="document.getElementById('Driver_license').style.display='none'">No</label></div>
                </div>
            </div>

            <div class="editfield" id="Driver_license" style="display:none">
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class1" value="Class1">Class 1 
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class2" value="Class2">Class 2 
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class2A" value="Class2A">Class 2A 
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class2B" value="Class2B">Class 2B <br>
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class3" value="Class3">Class 3 
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class3A" value="Class3A">Class 3A 
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class3C" value="Class3C">Class 3C 
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class3CA" value="Class3CA">Class 3CA <br>
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class4" value="Class4">Class 4 
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class4A" value="Class4A">Class 4A
              <input type="checkbox" name="Driver_license_class" id="Driver_license_class5" value="Class5">Class 5 
            </div>
            </p>
            {{-- End of Driver`s License --}}

            <div class="form-group">
                  {{Form::submit('Next Page', ['class' => 'btn btn-primary'])}}
            </div>

{!! Form::close() !!}

@endsection

migration file

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePersonalInfosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('personal_infos', function (Blueprint $table) {
            $table->increments('id');
            $table->String('Name');
            $table->String('Driver_license');
            $table->String('Driver_license_class');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('personal_infos');
    }
}

controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\PersonalInfo;

class PersonalInfoController extends Controller
{
          public function submit(Request $request){
        $this->validate($request,[
            'Name/NRIC' => 'required',
        ]);

        $PersonalParticulars = new PersonalInfo;
        $PersonalParticulars->Name = $request->input('Name/NRIC');
        $PersonalParticulars->Driver_license = $request->input('Driver_license');
        $PersonalParticulars->Driver_license_class = $request->input('Driver_license_class');

        $PersonalParticulars->save();
    return redirect('http://formapplication.dev/page2');
    }
}

Upvotes: 1

Views: 2676

Answers (2)

Vahid
Vahid

Reputation: 950

Change the name of checkboxes in your blade to

name="Driver_license_class[]"

So that you can get the selected licenses as an array in your php code.

To save an array to a field in database, you need to save it in CSV format:

$PersonalParticulars->Driver_license = implode(',', $request->input('Driver_license'));

Alternatively you may save data in JSON format

$PersonalParticulars->Driver_license = json_encode($request->input('Driver_license'));

Upvotes: 0

Huso
Huso

Reputation: 1486

You have to set it up as an array by adding [] in the name of the checkbox.

Change all <input type="checkbox" name="Driver_license_class" to:

<input type="checkbox" name="Driver_license_class[]"

Now, you will receive the results as an array, instead of just the single value. To process the values into the database, you could do mulitple things depending on how you want it to be saved.

You could loop trough the values by using the following code:

if(isset($_POST['Driver_license_class'])){
    foreach($_POST['Driver_license_class'] as $license){
        echo $license; // Outputs the selected license
    }
}

Or you can implode the values with the desired delimiter:

implode(", ", $_POST['Driver_license_class'])

This will return the selected results as comma seperated values.

Edit

As requested, here is where you need to change your code after adding the []:

$PersonalParticulars->Driver_license_class = implode(", ", $request->input('Driver_license_class'));

Upvotes: 1

Related Questions