简体   繁体   中英

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

具有“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"

在此处输入图像描述

I appreciate your help

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM