Home > Software engineering >  Insert unchecked checkbox value to database in Laravel 8
Insert unchecked checkbox value to database in Laravel 8

Time:07-23

I wrote code for an evaluation form related to quality management. I have a portion in which multiple checkboxes are set to be checked in the code. The database dynamically generates all the names, values, and ids.

@foreach ($scoreEvaluations as $scoreEvaluation)
    <div >
        <!-- radio -->
        <div >
            <div >
                <input  type="checkbox"
                       name="score_evaluation_id-{{ $scoreEvaluation->id }}"
                       value="{{ $scoreEvaluation->marks }}"
                       id="score_evaluation_id-{{ $scoreEvaluation->id }}"
                       onclick="score_check(this)"
                       checked>
                <label >
                    {{ $scoreEvaluation->name }}
                </label>
            </div>
        </div>
    </div>
@endforeach

From the above code, the form is submitted when I submit the form, but it only saves the value of the checked checkboxes. At the same time, I want to store the values of checked as well as unchecked checkboxes.

Controller

public function store(QCFeedbackRequest $request, QCFeedback $feedback)
{
    $feedback = $feedback->create($request->all());
    $this->addScoreResponse($request, $feedback);
    Session::flash('success', 'Feedback added successfully!');

    return redirect()->route('qc-feedback-audits.index');
}

public function addScoreResponse($request, $feedback)
{
    foreach ($request->all() as $key => $value) {

        $key_arr = explode('-', $key);
        if ($key_arr[0] == 'score_evaluation_id')  {
                $score_response = new ScoreEvaluationResponse;
                $score_response->score_evaluation_id = $key_arr[1];
                $score_response->qc_feedback_id = $feedback->id;
                $score_response->checkbox_status = 'Checked';
                $score_response->save();
        }
    }
}

The function store() is adding the code in the qc_feedbacks table, and the other function is to add the responses of the checkboxes in the table of score_evaluation_responses(having fields id, qc_feedback_id (foreign key of qc_feedbacks) and score_evaluation_id (another foreign key for score_evaluations table).

CodePudding user response:

If I got you right, you want to get both checked and unchecked checkbox values which means you want to get all input values in the form. Then why you need checkboxes actually? They are used for filtering data which means u may need only checked or unchecked values in ideal world. If you still want to get all checkbox values for some reason you can use additional hidden checkbox inputs like

<input id="score_evaluation_id-{{ $scoreEvaluation->id }}"
type="hidden" value="{{ $scoreEvaluation->marks }}"
name='score_evaluation_id-{{ $scoreEvaluation->id }}'>

<input id="score_evaluation_id-{{ $scoreEvaluation->id }}" 
type="checkbox" value="{{ $scoreEvaluation->marks }}" 
name="score_evaluation_id-{{ $scoreEvaluation->id }}">

With that logic you will get your desired value in backend whether checkbox is checked or not.

But if you want to get only unchecked values, then check out this issue - POST unchecked HTML checkboxes

  • Related