【问题标题】:Laravel: How to save on database from dynamic jquery checkboxLaravel:如何从动态 jquery 复选框中保存数据库
【发布时间】:2021-01-28 17:44:07
【问题描述】:

大家好!

我的 Laravel 项目有下一个问题

我有一个动态复选框,我使用 jquery 执行此操作,我的问题是当我尝试保存在我的数据库中时,这将我保存在一行中不同行上选择的所有复选框中的所有值,我使用 implode 保存在单元格中,但在所有单元格上保存相同的结果,“medicamento”和“dosis”的输入文本正常工作,但复选框“cuando1”不起作用。

Jquery 脚本

<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();
    }

数据库图像,检查列“cuando1”的所有值

带有选中复选框的刀片的图像,看到选中的复选框是保存在数据库中所有行上的内爆。

我想为 aspirina 1: Mañana、Tarde、aspirina 2: Mañana 和 aspirina 3: Noche 存钱

  • M = "玛娜娜"
  • T = “迟到”
  • N = "Noche"

感谢您的帮助

【问题讨论】:

    标签: php jquery laravel laravel-5.8 checkboxlist


    【解决方案1】:

    这是因为您在下面一行获得了所有复选框的值:

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

    在您的 html 文件中,使用 cuando[i][] 而不是 cuando[],其中 i 从零开始,并随着模板的每个循环而递增。

    然后在你的控制器中使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2017-01-29
      • 2012-05-11
      • 2015-01-11
      • 1970-01-01
      相关资源
      最近更新 更多