0

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 class="col-sm-2">
        <!-- radio -->
        <div class="form-group">
            <div class="form-check">
                <input class="form-check-input score" type="checkbox"
                       name="score_evaluation_id-{{ $scoreEvaluation->id }}"
                       value="{{ $scoreEvaluation->marks }}"
                       id="score_evaluation_id-{{ $scoreEvaluation->id }}"
                       onclick="score_check(this)"
                       checked>
                <label class="form-check-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).

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • checkboxes are only submitted if they are checked so if you want to pass that data you would have to use other input types in conjunction with the checkboxes – lagbox Jul 21 '22 at 22:02

1 Answers1

0

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

Vüsal Hüseynli
  • 889
  • 1
  • 4
  • 16