Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

php - Laravel: How to save on database from dynamic jquery checkbox

Good day everyone!

I have the next issue on my Laravel project

I have a dynamic checkbox that I do it with jquery, my issue is when I try to save on my database, this save me all the values from all the checkbox selected on different lines in one line, I use implode to save in the cell, but save me the same result on all the cells, the input text for "medicamento" and "dosis" working correctly, but the checkbox "cuando1" is not working.

Jquery Script

<script id="document-template" type="text/x-handlebars-template">
        <tr class="delete_add_more_item" id="delete_add_more_item">
            <td style="width:300px;border: 5px solid transparent"><input type="text" class="form-control" name="medicamentos[]" aria-describedby="medicamentos" id="medicamentos" placeholder="Medicamentos Recetados"></td>
            <td style="width:100px;border: 5px solid transparent"><input type="text" class="form-control" name="dosis[]" aria-describedby="dosis" id="dosis" placeholder="Dosis"></td>
            <td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando1" value="Ma?ana"></label></td>
            <td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando2" value="Tarde"></label></td>
            <td style="width:50px;border: 5px solid transparent"><label><input type="checkbox" name="cuando[]" id="cuando3" value="Noche"></label></td>
            <td>
                <button type="button" class="btn btn-danger"><i class="fa fa-minus fa-2x removeaddmore" style="cursor:pointer;color:white;"></i></button>
            </td>
        </tr>
    </script>
    <script type="text/javascript">

        $(document).on('click','#addMore',function(){

            $('.table').show();
            var source = $("#document-template").html();
            var template = Handlebars.compile(source);

            var data = {
                medicamentos: medicamentos,
                dosis: dosis,
                cuando1: cuando1,
                cuando2: cuando2,
                cuando3: cuando3,
            }

            var html = template(data);
            $("#addRow").append(html)
        });

        $(document).on('click','.removeaddmore',function(event){
            $(this).closest('.delete_add_more_item').remove();
        });
    </script>

Controller.php

$number = count($request->medicamentos);
    for ($i=0; $i < $number; $i++) {
       
        $cuando1 = implode(', ', (array) $request->get('cuando'));
            $Medicamentos = new Medicamentos();
            $Medicamentos->idpaciente = $idpaciente;
            $Medicamentos->medicamentos = $request->medicamentos[$i];
            $Medicamentos->fecha = $hoy;
            $Medicamentos->dosis = $request->dosis[$i];
            $Medicamentos->cuando1 = $cuando1;
            $Medicamentos->save();
    }

Image of database,check the column "cuando1" with all the values

Database with column "cuando"

Image of my blade with the selected checkbox, see that the Selected Checkbox are the saved on all the rows in the database with the implode.

I want to save for aspirina 1: Ma?ana, Tarde, for aspirina 2: Ma?ana and for aspirina 3: Noche

  • M = "Ma?ana"
  • T = "Tarde"
  • N = "Noche"

enter image description here

I appreciate your help

question from:https://stackoverflow.com/questions/65942479/laravel-how-to-save-on-database-from-dynamic-jquery-checkbox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's because your getting all of the values of checkboxes on line below:

$cuando1 = implode(', ', (array) $request->get('cuando'));

In your html file, instead of cuando[] use cuando[i][] where i starts from zero and gets incremented with each loop of template.

Then in your controller use :

$cuando1 = implode(', ', (array) $request->cuando[$i]);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...