vendredi 19 juin 2020

Laravel insert text box value and checkbox group if checked

I want to submit all input fields value and the checkbox value if checkbox is checked.

right now if i am submitting the form its saved all input text values with checkbox value 1 but i want checkbox value 1 only when checkbox is checked otherwise default value is 0

html / blade form:

    Answer: <input id="answer" type="text" name="answer[]">      
    if Correct: <input id="correct" value="1" type="checkbox" name="correct[]">

php

$question = Question::create([
        'quiz_id' => $data['quiz_id'],
        'question' => $data['question'],
    ]);

        for ($i = 0; $i < count($data['answer']); $i++) {
        $answer = new Answer();
        if (!empty($data['answer'][$i])) {
            $answer->answer = $data['answer'][$i];


        }

        if (!empty($data['correct'][$i])) {

            $answer->correct = $data['correct'][$i];
        }

        $question->answer()->save($answer);
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire